女朋友让我处理照片,使用python几行代码实现自动抠图!必看!

安装RemoveBg
pip install RemoveBg
在这里插入图片描述
安装成功!

批量处理图片实现抠图

上代码:

#coding:utf-8
from removebg import RemoveBg
import os
rmbg = 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')

猜你喜欢

转载自blog.csdn.net/m0_48915964/article/details/109016083