python removebg 抠图
removebg是一家号称可以5s抠图的网站,支持使用api传入图片文件或图片链接抠图。个人用户每月免费50张,就算有大量图片需要处理,价格真心不高。那么如果利用removebg进行淘宝抠图服务,应该能赚一点~
前期准备
- removebg帐号一个(没有注册)
- requests库
代码示例
-
传入图片文件
import requests api_key = input("你自己的api密钥") image_path = input("本地图片路径:") response = requests.post( 'https://api.remove.bg/v1.0/removebg', files={'image_file': open(image_path, 'rb')}, data={'size': 'auto'}, headers={'X-Api-Key': api_key}, ) if response.status_code == requests.codes.ok: with open('no-bg-' + image_path, 'wb') as out: out.write(response.content) else: print("Error:" + response.status_code + response.text)
-
传入图片链接
import requests api_key = input("你自己的api密钥") image_url = input("本地图片路径:") response = requests.post( 'https://api.remove.bg/v1.0/removebg', files={'image_file': open(image_url, 'rb')}, data={'size': 'auto'}, headers={'X-Api-Key': api_key}, ) if response.status_code == requests.codes.ok: with open('no-bg-' + image_url, 'wb') as out: out.write(response.content) else: print("Error:" + response.status_code + response.text)
简单整合
#简单整合难免出错,大佬多指教
import requests
while True:
chose = input(
"""
***************************************
* 欢迎使用本工具 skye*
* 工具是调用www.remove.bg的API实现抠图 *
* 因此自行前往网站注册申请API密钥 *
* 个人账户每月免费50张 *
*=====================================*
* 开始去除背景 请按 1 *
* 退出工具 请按 0 *
***************************************
"""
)
if chose == '1':
api_key = input("请填入你的API密钥:\n")
while True:
chose = input("""
**********************************
* 本地图片 请按 1 *
* 网络图片 请按 2 *
**********************************
""")
if chose == '1':
print("""
_______________________________________
请将处理图片放于本工具同一目录之下!!!
请将处理图片放于本工具同一目录之下!!!
请将处理图片放于本工具同一目录之下!!!
---------------------------------------
""")
image_path = input("请填入本地图片名称:\n")
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open(image_path, 'rb')},
data={'size': 'auto'},
headers={'X-Api-Key': api_key},
)
if response.status_code == requests.codes.ok:
with open('no-bg-' + image_path, 'wb') as out:
out.write(response.content)
else:
print("Error:" + response.status_code + response.text)
with open("removebg_error_logs.txt", 'rt', encoding="utf-8") as logs:
logs.write("Error:" + response.status_code + response.text + "\n")
exit()
elif chose == '2':
image_url = input("请填入图片的URL链接:\n")
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_url': image_url},
data={'size': 'auto'},
headers={'X-Api-Key': api_key},
)
if response.status_code == requests.codes.ok:
with open('no-bg-' + image_url, 'wb') as out:
out.write(response.content)
else:
print("Error:", response.status_code, response.text)
with open("removebg_error_logs.txt", 'rt', encoding="utf-8") as logs:
logs.write("Error:" + response.status_code + response.text + "\n")
exit()
else:
print("输入有误请重新输入~~~")
elif chose == '0':
print("欢迎下次使用~~~")
break
else:
print("输入有误请重新输入~~~")