制作证件照我们主要有两个工作:
1. 修改照片背景
2. 修改照片尺寸
修改背景我们需要用到第三方库 removebg
,修改照片尺寸需要用到 PIL
库,这两个库的安装使用 pip install removebg
和 pip install Pillow
即可。
removebg涉及到api_key,需要到其官网注册并申请:
https://accounts.kaleido.ai/users/sign_up
接下来通过代码demo如何制作证件照
#encoding=utf-8
from PIL import Image
from removebg import RemoveBg
api_key='9Esz4y4H9UKzChH7hpxxxx' #你的api_key
def change_bgcolor(file_in, file_out, api_key, color):
'''
#必须为png格式
'''
p,s=file_in.split(".")
rmbg = RemoveBg(api_key, 'error.log')
rmbg.remove_background_from_img_file(file_in)
file_no_bg = "{}.{}_no_bg.{}".format(p,s,s)
no_bg_image = Image.open(file_no_bg)
x, y =