python工具——removebg

removebg是一个利用AI智能抠图的网站

提供了API 接口,可以直接调用并实现抠图,每月有50张免费

使用前需要去官网 https://www.remove.bg/ 注册并获取API KEY

安装removebg库

pip install removebg

安装pillow

pip install pillow

eg:

原图

抠图

from removebg import RemoveBg

rmbg = RemoveBg("XXXXXXX", "error.log")
rmbg.remove_background_from_img_file("1.png")

生成图片

 换背景色

from removebg import RemoveBg
from PIL import Image

rmbg = RemoveBg("kfKrPF2o8LGv1RBURitZdwBL", "error.log")
file_in = 'D:\python\demo\1.png'
file_out = 'D:\python\demo\11.png'
# 换背景色
color = (0, 125, 255)
p, s = file_in.split(".")
rmbg.remove_background_from_img_file(file_in)
file_no_bg = "{}.{}_no_bg.{}".format(p, s, s)
no_bg_image = Image.open(file_no_bg)
x, y = no_bg_image.size
new_image = Image.new('RGBA', no_bg_image.size, color=color)
new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)
new_image.save(file_out)

 注:

  可以用来生成证件照

原文地址:https://www.cnblogs.com/baby123/p/15000322.html