测试了好几个代码,发现还是这个AI的最完美,最蛋疼的是 有各种限制。
偶尔用一次还是可以的
https://www.remove.bg/zh/
注册账号,即可使用API
from removebg import RemoveBg
rmbg = RemoveBg("YOUR-API-KEY", "error.log")
rmbg.remove_background_from_img_file("4.jpg", bg_color='red')#bg_color不写则为透明背景
#rmbg.remove_background_from_img_url("http://www.example.com/some_image.jpg")网络图片
- img_file_path 图像文件的路径
- size 输出图像的大小(regular=0.25 MP,HD=4MP,4k=最多10
MP)。默认值为“regular” - bg_color 为图片添加纯色背景。可以是十六进制颜色代码(例如:81d4fa,FFF)或颜色名称(例如green)。
官方代码
# Requires "requests" to be installed (see python-requests.org)
import requests
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open('/path/to/file.jpg', 'rb')},
data={'size': 'auto'},
headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
with open('no-bg.png', 'wb') as out:
out.write(response.content)
else:
print("Error:", response.status_code, response.text)