removebg抠图小工具

由于比较简单,直接上代码(removebg接口官网),更多小工具获取

(1)官网API,需注册获取X-Api-Key:removebg_官网api.py  
 1 import requests
 2 response = requests.post(
 3     'https://api.remove.bg/v1.0/removebg',
 4     files={'image_file': open('/path/to/file.jpg', 'rb')},
 5     data={'size': 'auto'},
 6     headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
 7 )
 8 
 9 if response.status_code == requests.codes.ok:
10     with open('no-bg.png', 'wb') as out:
11         out.write(response.content)
12 else:
13     print("Error:", response.status_code, response.text)
removebg_官网api.py
(2)单个图片抠图removebg_one.py
1 #https://www.remove.bg/api
2 #pip install removebg
3 
4 from removebg import RemoveBg
5 rmbg = RemoveBg("DG2WMZrZNnU2oG8fb7mzv6Ja", "error.log") # 引号内是你获取的API
6 rmbg.remove_background_from_img_file(r"E:\Python项目\总结复习\抠图removebg_\images\1.jpg",size="4k") #图片地址
removebg_one.py
(3)批量抠图removebg_more.py
1 from removebg import RemoveBg
2 import os
3 
4 rmbg = RemoveBg("DG2WMZrZNnU2oG8fb7mzv6Ja", "error.log")# 引号内是你获取的API
5 path = os.path.join(os.getcwd(),'images')#图片放到程序的同级文件夹images 里面
6 # print(os.listdir(path))
7 for pic in os.listdir(path):
8     rmbg.remove_background_from_img_file(f"{path}\{pic}")
removebg_more.py

猜你喜欢

转载自www.cnblogs.com/open-yang/p/11240053.html