申请remove.bg的APIKey
在B站偶然发现https://www.remove.bg/zh/upload这个神奇的网站
抠图竟然比我用ps扣的还好,还完全免费,还开源!!!
但是碍于我有时候抠图比较多,所以作为程序员的我肯定是不会做这种重复点击的啥动作的
原理:把需要处理的图片放入文件夹,输出保存到新的文件夹
注册/登录账号获取apikey:https://accounts.kaleido.ai/users/sign_in#api-key
但是有一个缺点: 每个apikey每个月只有50次免费机会,且用且珍惜,网页无限次
码代码
让我们看看它的案例:https://github.com/brilam/remove-bg/blob/master/removebg/removebg.py
呦西,还算简单,来一波现学现卖
这里只做了我需要的需求,本地img抠图,他还能输入img的url进行抠图后再保存等等
#!/usr/bin/env python
# -*- coding: utf-8 -*-import requests
import requests
API_ENDPOINT = "https://api.remove.bg/v1.0/removebg"
class RemoveBg():
def __init__(self, api_key):
self.__api_key = api_key
# 文件大小:最大 12 MB
# img_file_path : 需要抠图的图片路径
# out_file_path : 扣好的图片保存路径
# size参数的解释如下
# size: `'auto'` = highest available resolution, `'preview'`|`'small'`|`
# 'regular'` = 0.25 MP, `'medium'` = 1.5 MP, `'hd'` = 4 MP, `'full'`|`'4k'` = original size)
# bg_color 实例: "green"背景绿色 "yellow"背景黄色 None无背景(这个None不用引号,其他颜色要引号)
def remove_background_from_img_file(self, img_file_path, out_file_path, size="auto", bg_color=None):
# Open image file to send information post request and send the post request
img_file = open(img_file_path, 'rb')
response = requests.post(
API_ENDPOINT,
files={
'image_file': img_file},
data={
'size': size,
'bg_color': bg_color
},
headers={
'X-Api-Key': self.__api_key})
response.raise_for_status()
self.__output_file__(response, out_file_path)
# Close original file
img_file.close()
def __output_file__(self, response, new_file_name):
# If successful, write out the file
if response.status_code == requests.codes.ok: