一次悲催的经历,作为程序员的我,不会 Photoshop
  在帮朋友换证件照的底色时,成品惨不忍睹呀😂😂😂
  当然啦,不可能就这么放弃了,这可不是我的行事风格呀,于是脑子中浮现一个身影“程序员”,对呀我是搞程序的,搞软件的为何不自己写一个的出来呢!!!
  在各种不服的心理驱动下,我拿出了我的秘密武器Python,我决定做一个自动换背景的小工具,但光有这还不行。所谓站在巨人的肩上才能看得更远,于是乎我找到了​​​Remove​​​这个图像处理库。
  毕竟是我自己用,也就没必要搞个GUI程序咯,搞个控制台程序跑起来就行啦
  就这样那不知道要学多少PS技巧、做多少工作的抠图换背景的大量工作,就这么的自动化的完成了,而且效果显著

  • 下面是我临时写出的代码,有不足之处请自行优化
# -*- coding: utf-8 -*-
"""
@Time : 2020/9/14 22:45
@File : 图片换底色.py
@Software: PyCharm
"""

import os
import requests
from future.moves import tkinter
from future.moves.tkinter import filedialog


def open_img_file_path():
"""
获取图片路径
:return:
"""
root = tkinter.Tk() # 创建一个Tkinter.Tk()实例
root.withdraw() # 将Tkinter.Tk()实例隐藏
default_dir = r"文件路径"
file_path = filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
if len(file_path) != 0:
remove_bg(file_path)
else:
SystemExit()


def remove_bg(file_path):
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open(file_path, 'rb')},
data={'size': 'auto',
# 证件照底色蓝底:#438EDB ,红红底:#FF0000 ,白底:#FFFFFF
'bg_color': input('证件照底色蓝底:#438EDB ,红红底:#FF0000 ,白底:#FFFFFF\n')
},
# * 号 所在引号内填入 Api-Key
headers={'X-Api-Key': 'w2**********************'},
)
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)


if __name__ == '__main__':
open_img_file_path()

代码中的密钥可点击下方的地址获取:

Api-Key(API密钥)