0%

python证件照制作

python证件照制作

临时需要用到蓝色两寸的证件照,电脑上又没有装ps,并且ps也不是很擅长,从网上找了发现有在线制作的,但是收费很贵,就想到这种在线的肯定是通过技术实现的,笔者作为一个开发想的是,你能做到 我也行,然后实验下发下 全自己写有点麻烦,找到个第三方库 很好用,记录一下

remove.bg:图片背景消除 一个很有用的网站,可以一键去除图片背景,且免费(自己用够了,50张免费图片),并且提供api,可以直接调用

获取api秘钥

调用接口虽然是免费的但是也要注册账号,然后获取api秘钥,注册

安装removebg扩展

removebg的api是http的接口,可以使用各种语言调用,笔者这里使用的是python,直接安装扩展,这里安装了两个扩展 RemoveBg,Pillow

1
2
pip install Pillow
pip install RemoveBg
证件照制作

不多说直接上代码

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
31
32
33
34
35
36
37
38
39
import os

from PIL import Image
from removebg import RemoveBg
import cv2

#color 可以是 "red","blue" 或者 (255, 0, 0) 这种rgb参数
def image_matting(old_image_path, new_image_path, api_key, color):
# API KEY获取官方网站:https://www.remove.bg/r/UU4eRteAvmnH9n8etSokYDVq?locale=zh
#去除背景
rmbg = RemoveBg(api_key, "error.log")
rmbg.remove_background_from_img_file(old_image_path)

#重新上背景色
parent_path = os.path.dirname(old_image_path)
old_image_name = os.path.split(old_image_path)[-1]
no_bg_image_name = old_image_name + "_no_bg.png"
if no_bg_image_name in os.listdir(parent_path):
no_bg_image_path = parent_path + "/" + no_bg_image_name
no_bg_image = Image.open(no_bg_image_path)
x, y = no_bg_image.size
try:
new_image = Image.new('RGBA', no_bg_image.size, color=color)
new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)
new_image.save(new_image_path)
except:
print("image matting except")
else:
print("image matting fail")

def resize(old_image_path, new_image_path, width, height):
img = cv2.imread(old_image_path)

# 缩小图片
img = cv2.resize(img, (width, height))
cv2.imwrite(new_image_path, img)

resize("1.jpg", "small_2.jpg", 412, 531)
image_matting("small_2.jpg", "small_blue.jpg", "blue")
生成6寸相纸大小的多张图

打印相片每次都是用大的相纸,从网上找个个代码可以 生成6寸相纸的图片,好几个网站有,不知道那个是原创的 所有没有写转的来自哪里,如果原作者看到,可以联系我 我加上您的原地址,不多说直接上代码

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#Author:ZM

"""
照片尺寸,宽*高(单位:像素)
1寸照片:295*413
2寸照片:413*626
5寸照片(横版):1500*1050
6寸照片(横版):1800*1200
"""
from PIL import Image,ImageDraw

WIDTH_1IN = 295
HEIGHT_1IN = 413

WIDTH_2IN = 413
HEIGHT_2IN = 626

WIDTH_5IN = 1500
HEIGHT_5IN = 1050

# 非全景6寸照片
WIDTH_6IN = 1950
HEIGHT_6IN = 1300

def cut_photo(photo,choice):
"""
将照片按照比例进行裁剪成1寸、2寸
:param photo: 待处理的照片
:param choice: <int> 1表明1寸,2表明2寸
:return: 处理后的照片
"""
width = photo.size[0] # 宽
height = photo.size[1] #高
rate = height / width
if choice == 1:
if rate < (HEIGHT_1IN/WIDTH_1IN):
x = (width - int(height / HEIGHT_1IN * WIDTH_1IN)) / 2
y = 0
cutted_photo = photo.crop((x, y, x + (int(height / HEIGHT_1IN * WIDTH_1IN)), y + height))

else:
x = 0
y = (height - int(width / WIDTH_1IN * HEIGHT_1IN)) / 2
cutted_photo = photo.crop((x, y, x + width, y + (int(width / WIDTH_1IN * HEIGHT_1IN))))
return cutted_photo

if choice == 2:
if rate < (HEIGHT_2IN/WIDTH_2IN):
x = (width - int(height / HEIGHT_2IN * WIDTH_2IN)) / 2
y = 0
cutted_photo = im.crop((x, y, x + (int(height / HEIGHT_2IN * WIDTH_2IN)), y + height))

else:
x = 0
y = (height - int(width / WIDTH_2IN * HEIGHT_2IN)) / 2
cutted_photo = im.crop((x, y, x + width, y + (int(width / WIDTH_2IN * HEIGHT_2IN))))

return cutted_photo

def resize_photo(photo,choice):
'''
缩放照片
:param photo: 待处理的照片
:param choice: <int> 1表明1寸,2表明2寸
:return: 处理后的照片
'''
if choice == 1:
resized_photo = photo.resize((WIDTH_1IN,HEIGHT_1IN))
return resized_photo
if choice == 2:
resized_photo = photo.resize((WIDTH_2IN, HEIGHT_2IN))
return resized_photo


def layout_photo_5_1(photo):
"""
在5寸照片上排版1寸照片
:param photo: 待处理照片1寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [WIDTH_5IN,HEIGHT_5IN], (255,255,255))
draw = ImageDraw.Draw(bk)# 建立画笔
draw.line([(0,HEIGHT_5IN/2),(WIDTH_5IN,HEIGHT_5IN/2)],fill=128) # 横线
draw.line([(WIDTH_5IN*0.25,0),(WIDTH_5IN*0.25,HEIGHT_5IN)],fill=128) # 第1条竖线
draw.line([(WIDTH_5IN*0.5,0),(WIDTH_5IN*0.5,HEIGHT_5IN)],fill=128) # 第2条竖线
draw.line([(WIDTH_5IN*0.75,0),(WIDTH_5IN*0.75,HEIGHT_5IN)],fill=128) # 第3条竖线

focus_point = [0.125 * WIDTH_5IN,0.25 * HEIGHT_5IN]
start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN]
for i in range(0,2):
for k in range(0,4):
bk.paste(photo, (int(start_point[0] + (k * WIDTH_5IN / 4)), int(start_point[1] + 0.5 * i * HEIGHT_5IN)))
return bk


def layout_photo_5_2(photo):
"""
在5寸照片上排版2寸照片
:param photo: 待处理照片2寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [HEIGHT_5IN,WIDTH_5IN], (255,255,255)) # 竖版排版
# 建立画笔
draw = ImageDraw.Draw(bk)
draw.line([(0,WIDTH_5IN/2),(WIDTH_5IN,WIDTH_5IN/2)],fill=128) # 横线
draw.line([(HEIGHT_5IN*0.5,0),(HEIGHT_5IN*0.5,WIDTH_5IN)],fill=128) # 竖线
focus_point = [0.25 * HEIGHT_5IN, 0.25 * WIDTH_5IN]
start_point = [focus_point[0] - 0.5 * WIDTH_2IN, focus_point[1] - 0.5 * HEIGHT_2IN]
#print(focus_point,start_point)
for i in range(0,2):
for k in range(0,2):
bk.paste(photo, (int(start_point[0] + (k * HEIGHT_5IN / 2)), int(start_point[1] + 0.5* i * WIDTH_5IN)))
return bk

def layout_photo_5_mix(photo1,photo2):
"""
在5寸照片上混合排版1寸、2寸照片
:param photo1: 待处理照片1寸
:param photo1: 待处理照片2寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [WIDTH_5IN,HEIGHT_5IN], (255,255,255))
# 建立画笔
draw = ImageDraw.Draw(bk)
draw.line([(0,HEIGHT_5IN/2),(WIDTH_5IN,HEIGHT_5IN/2)],fill=128) # 横线
draw.line([(WIDTH_5IN*0.25,0),(WIDTH_5IN*0.25,HEIGHT_5IN)],fill=128) # 第1条竖线
draw.line([(WIDTH_5IN*0.5,0),(WIDTH_5IN*0.5,HEIGHT_5IN)],fill=128) # 第2条竖线

focus_point = [0.125 * WIDTH_5IN,0.25 * HEIGHT_5IN]
start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN]
focus_point2 = [0.75 * WIDTH_5IN, 0.25 * HEIGHT_5IN]
start_point2 = [focus_point2[0] - 0.5 * HEIGHT_2IN, focus_point2[1] - 0.5 * WIDTH_2IN]

for i in range(0,2):
for k in range(0,2):
bk.paste(photo1, (int(start_point[0] + (k * WIDTH_5IN / 4)), int(start_point[1] + 0.5 * i * HEIGHT_5IN)))

bk.paste(photo2,(int(start_point2[0]),int(start_point2[1])))
bk.paste(photo2,(int(start_point2[0]),int(start_point2[1] + 0.5 * HEIGHT_5IN)))
return bk

def layout_photo_6_1(photo):
"""
在6寸照片上排版2寸照片
:param photo: 待处理照片1寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [HEIGHT_6IN,WIDTH_6IN], (255,255,255)) # 竖版排版
# 建立画笔
draw = ImageDraw.Draw(bk)
draw.line([(0,WIDTH_6IN*0.25),(WIDTH_6IN,WIDTH_6IN*0.25)],fill=128) # 横线
draw.line([(0,WIDTH_6IN*0.5),(WIDTH_6IN,WIDTH_6IN*0.5)],fill=128) # 横线
draw.line([(0,WIDTH_6IN*0.75),(WIDTH_6IN,WIDTH_6IN*0.75)],fill=128) # 横线
draw.line([(HEIGHT_6IN*0.25,0),(HEIGHT_6IN*0.25,WIDTH_6IN)],fill=128) # 竖线
draw.line([(HEIGHT_6IN*0.5,0),(HEIGHT_6IN*0.5,WIDTH_6IN)],fill=128) # 竖线
draw.line([(HEIGHT_6IN*0.75,0),(HEIGHT_6IN*0.75,WIDTH_6IN)],fill=128) # 竖线
focus_point = [0.125 * HEIGHT_6IN, 0.125 * WIDTH_6IN]
start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN]
#print(focus_point,start_point)
for i in range(0,4):
for k in range(0,4):
bk.paste(photo, (int(start_point[0] + (k * HEIGHT_6IN / 4)), int(start_point[1] + i * 0.25 * WIDTH_6IN )))
return bk

def layout_photo_6_2(photo):
"""
在6寸照片上排版2寸照片
:param photo: 待处理照片2寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [WIDTH_6IN,HEIGHT_6IN], (255,255,255))
# 建立画笔
draw = ImageDraw.Draw(bk)
draw.line([(0,HEIGHT_6IN/2),(WIDTH_6IN,HEIGHT_6IN/2)],fill=128) # 横线
draw.line([(WIDTH_6IN*0.25,0),(WIDTH_6IN*0.25,HEIGHT_6IN)],fill=128) # 第1条竖线
draw.line([(WIDTH_6IN*0.5,0),(WIDTH_6IN*0.5,HEIGHT_6IN)],fill=128) # 第2条竖线
draw.line([(WIDTH_6IN*0.75,0),(WIDTH_6IN*0.75,HEIGHT_6IN)],fill=128) # 第3条竖线
focus_point = [0.125 * WIDTH_6IN,0.25 * HEIGHT_6IN]
start_point = [focus_point[0] - 0.5 * WIDTH_2IN, focus_point[1] - 0.5 * HEIGHT_2IN]
for i in range(0,2):
for k in range(0,4):
bk.paste(photo, (int(start_point[0] + (k * WIDTH_6IN / 4)), int(start_point[1] + 0.5 * i * HEIGHT_6IN)))
return bk


def layout_photo_6_mix1(photo1,photo2):
"""
在6寸照片上混合排版1寸、2寸照片
:param photo1: 待处理照片1寸
:param photo1: 待处理照片2寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [WIDTH_6IN,HEIGHT_6IN], (255,255,255))
# 建立画笔
draw = ImageDraw.Draw(bk)
draw.line([(0,HEIGHT_6IN*0.5),(WIDTH_6IN,HEIGHT_6IN/2)],fill=128) # 横线
draw.line([(0,HEIGHT_6IN*0.25),(WIDTH_6IN*0.5,HEIGHT_6IN*0.25)],fill=128) # 短横线
draw.line([(0,HEIGHT_6IN*0.75),(WIDTH_6IN*0.5,HEIGHT_6IN*0.75)],fill=128) # 短横线
draw.line([(WIDTH_6IN*0.25,0),(WIDTH_6IN*0.25,HEIGHT_6IN)],fill=128) # 第1条竖线
draw.line([(WIDTH_6IN*0.5,0),(WIDTH_6IN*0.5,HEIGHT_6IN)],fill=128) # 第2条竖线
draw.line([(WIDTH_6IN*0.75,0),(WIDTH_6IN*0.75,HEIGHT_6IN)],fill=128) # 第3条竖线
focus_point = [0.125 * WIDTH_6IN, 0.125 * HEIGHT_6IN]
start_point = [focus_point[0] - 0.5 * HEIGHT_1IN, focus_point[1] - 0.5 * WIDTH_1IN]
for i in range(0,4):
for k in range(0,2):
bk.paste(photo1, (int(start_point[0] + (0.25 * k * WIDTH_6IN )), int(start_point[1] + 0.25 * i * HEIGHT_6IN)))
focus_point2 = [0.625 * WIDTH_6IN, 0.25 * HEIGHT_6IN]
start_point2 = [focus_point2[0] - 0.5 * WIDTH_2IN, focus_point2[1] - 0.5 * HEIGHT_2IN]
for i in range(0,2):
for k in range(0,2):
bk.paste(photo2,(int(start_point2[0] + (0.25 * k * WIDTH_6IN)), int(start_point2[1] + 0.5 * i * HEIGHT_6IN)))
bk.show()
return bk



def layout_photo_6_mix2(photo1,photo2):
"""
在6寸照片上混合排版1寸、2寸照片
:param photo1: 待处理照片1寸
:param photo1: 待处理照片2寸
:return: 处理后的照片
"""
bk = Image.new("RGB", [HEIGHT_6IN,WIDTH_6IN], (255,255,255)) # 竖版排版
# 建立画笔
draw = ImageDraw.Draw(bk)

draw.line([(350,0),(350,WIDTH_6IN)],fill=128) # 竖线
draw.line([(700,0),(700,WIDTH_6IN)],fill=128) # 竖线


draw.line([(0,WIDTH_6IN*0.25),(700,WIDTH_6IN*0.25)],fill=128) # 横线1
draw.line([(0,WIDTH_6IN*0.5),(700,WIDTH_6IN*0.5)],fill=128) # 横线2
draw.line([(0,WIDTH_6IN*0.75),(700,WIDTH_6IN*0.75)],fill=128) # 横线3
draw.line([(700,WIDTH_6IN/3),(HEIGHT_6IN,WIDTH_6IN/3)],fill=128) # 横线4
draw.line([(700,WIDTH_6IN*2/3),(HEIGHT_6IN,WIDTH_6IN*2/3)],fill=128) # 横线5

focus_point = [0.5 * 350, 0.125 * WIDTH_6IN]
start_point = [focus_point[0] - 0.5 * WIDTH_1IN, focus_point[1] - 0.5 * HEIGHT_1IN]

#print(focus_point,start_point)
for i in range(0,4):
for k in range(0,2):
bk.paste(photo1, (int(start_point[0] + (k * 350)), int(start_point[1] + i * 0.25 * WIDTH_6IN )))

focus_point2 = [0.5 * HEIGHT_6IN+350, WIDTH_6IN/6]
start_point2 = [focus_point2[0] - 0.5 * WIDTH_2IN, focus_point2[1] - 0.5 * HEIGHT_2IN]
for i in range(0,3):
bk.paste(photo2, (int(start_point2[0]), int(start_point2[1] + i * WIDTH_6IN /3)))
return bk


im = Image.open('blue-white-removebg-preview.png')
width = im.size[0]
height = im.size[1]
rate = height / width
layout_photo_5_1(resize_photo(cut_photo(im,1),1)).save('5_1.jpg')
layout_photo_5_2(resize_photo(cut_photo(im,2),2)).save('5_2.jpg')
layout_photo_6_1(resize_photo(cut_photo(im,1),1)).save('6_1.jpg')
layout_photo_6_2(resize_photo(cut_photo(im,2),2)).save('6_2.jpg')
layout_photo_5_mix(resize_photo(cut_photo(im,1),1),resize_photo(cut_photo(im,2),2).rotate(90,expand=True)).save('5_1_mix.jpg')
layout_photo_6_mix1(resize_photo(cut_photo(im,1),1).rotate(90,expand=True),resize_photo(cut_photo(im,2),2)).save('6_mix1.jpg')
layout_photo_6_mix2(resize_photo(cut_photo(im,1),1),resize_photo(cut_photo(im,2),2)).save('6_mix2.jpg')