制作证件照我们主要有两个工作:
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 = no_bg_image.

本文介绍了如何使用Python进行证件照制作,包括更换照片背景和调整尺寸。主要涉及的库有removebg和PIL,安装它们可以轻松实现证件照处理。通过提供的代码示例,可以将原始照片转化为符合要求的证件照。
最低0.47元/天 解锁文章
1137





