将证件照杂乱背景改为指定纯色背景,同时修改照片尺寸。
所用到的库是rembg
详细代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from PIL import Image
import rembg
def process_image(input_path):
# 打开源图像
input_img = Image.open(input_path)
# 移除背景
output = rembg.remove(input_img)
# 以 4:3 的比例计算宽度.
new_width = int(output.height * 3 / 4)
# 计算起始X坐标的裁剪,以确保受试者保持居中。
x_start = (output.width - new_width) // 2
# 裁剪图像。
cropped_output = output.crop((x_start, 0, x_start + new_width, output.height))
# 创建一个带有裁剪尺寸的新图像,并用蓝色填充。
blue_background = Image.new("RGB", cropped_output.size, "#438EDB") # #438EDB为颜色代码
# 将裁剪的图像粘贴到蓝色背景上。
blue_background.paste(cropped_output, (0, 0), cropped_output)
# 保存结果图像。
inputname = input_path.split(".")[0]
blue_background.save(f'out_{inputname}.jpg')
# 调用函数
input_path = 'tsett.JPG'
process_image(input_path)
|



参考https://juejin.cn/post/7250291603814694970
1.一寸证件照的比例一般为5:7,尺寸是2.53.5cm,像素为295413。
2.二寸证件照的比例一般为4:3,尺寸是3.55.3cm,像素为413626。
https://id-photo.cn/color/
唯一的顾虑是网站是否将你提交(通过抓包,该网站的抠图换色是在服务端完成的)的照片和你的客户端信息绑定,然后可以精准的溯源你提交照片的IP地址。