-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_10_VerificationCode.py
More file actions
32 lines (24 loc) · 906 Bytes
/
Copy patht_10_VerificationCode.py
File metadata and controls
32 lines (24 loc) · 906 Bytes
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
from PIL import Image
from PIL import ImageDraw , ImageFont
from random import randint
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 生成随机点坐标
def randomPoint():
return randint(0, img.width), randint(0, img.height)
def randomColor(start=0, end=255):
return randint(start, end), randint(start, end),randint(start, end)
for i in range(img.width * img.height // 8):
draw.point(randomPoint(),randomColor(150))
myfont = ImageFont.truetype('Chalkduster.ttf', size=20)
for i in range(4):
res = []
txt = chr(randint(65,90))
res.append(txt)
draw.text((i * 30 + 10, 5), txt, fill=randomColor(0, 150), font=myfont)
res = ''.join(res)
for i in range(4):
draw.line((randomPoint()) + img.size, fill=randomColor(0, 150))
img.show()
with open('verification_code.png', 'wb') as f:
img.save(f, 'png')