forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0010.py
More file actions
48 lines (37 loc) · 1.25 KB
/
Copy path0010.py
File metadata and controls
48 lines (37 loc) · 1.25 KB
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
#!/usr/bin/python
#coding=utf-8
"""
第 0010 题:使用 Python 生成字母验证码图片
"""
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
IMAGE_MODE = 'RGB'
IMAGE_BG_COLOR = (255,255,255)
Image_Font = 'arial.ttf'
text = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz\
ABCDEFGHIJKLMNOPQRSTUVWXYZ',4))
def colorRandom():
return (random.randint(32,127),random.randint(32,127),random.randint(32,127))
#change 噪点频率(%)
def create_identifying_code(strs, width=400, height=200, chance=2):
im = Image.new(IMAGE_MODE, (width, height), IMAGE_BG_COLOR)
draw = ImageDraw.Draw(im)
#绘制背景噪点
for w in xrange(width):
for h in xrange(height):
if chance < random.randint(1, 100):
draw.point((w, h), fill=colorRandom())
font = ImageFont.truetype(Image_Font, 80)
font_width, font_height = font.getsize(strs)
strs_len = len(strs)
x = (width - font_width)/2
y = (height - font_height)/2
#逐个绘制文字
for i in strs:
draw.text((x,y), i, colorRandom(), font)
x += font_width/strs_len
#模糊
im = im.filter(ImageFilter.BLUR)
im.save('identifying_code_pic.jpg')
if __name__ == '__main__':
create_identifying_code(text)