9 lines of Python code to realize automatic image cutout

For those who know PhotoShop, it is a very simple operation to cut out a picture, and sometimes a picture can be cut out in a few seconds. However, for some more complicated pictures, sometimes it takes time to draw. Today, I will bring you a very fast and simple way to use Python to batch extract portraits.

First look at the original image:

9 lines of Python code to achieve automatic cutout!  Stop picturing yourself

Install RemoveBg
pip install RemoveBg

picture.png

Successful installation!

Batch process pictures to achieve cutout

Above code:

#coding:utf-8from removebg import RemoveBgimport osrmbg = RemoveBg("你的API密钥","error.log") 
#API密钥path = 'image'for pic in os.listdir(path):     
#os.listdir(path)的作用是返回path指定的文件夹包含的文件或文件夹的名字的列表    
img_path = os.path.join(path,pic)  
#把目录和文件名合成一个路径
 rmbg.remove_background_from_img_file(img_path)  
#去除背景
print(f'{img_path} is done')

See the result:

picture.png

Get it! How is it simple. Finally, let me say one more thing. I am a python development engineer. Here I have compiled a set of the latest python system learning tutorials, including basic python scripts to web development, crawler, data analysis, data visualization, machine learning, etc. . If you want these materials, you can follow the editor and send a private message to the editor in the background: "01" to get it.


The text and pictures in this article come from the Internet and their own ideas, which are only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for processing.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803440&siteId=291194637