1 Python生成随机验证码,需要使用PIL模块.
2
3 安装:
4
5 1
6 pip3 install pillow
7 基本使用
8
9 1. 创建图片
10
11
12 from PIL import Image
13 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
14
15 # 在图片查看器中打开
16 # img.show()
17
18 # 保存在本地
19 with open('code.png','wb') as f:
20 img.save(f,format='png')
21 2. 创建画笔,用于在图片上画任意内容
22
23
24 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
25 draw = ImageDraw.Draw(img, mode='RGB')
26 3. 画点
27
28
29 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
30 draw = ImageDraw.Draw(img, mode='RGB')
31 # 第一个参数:表示坐标
32 # 第二个参数:表示颜色
33 draw.point([100, 100], fill="red")
34 draw.point([300, 300], fill=(255, 255, 255))
35 4. 画线
36
37 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
38 draw = ImageDraw.Draw(img, mode='RGB')
39 # 第一个参数:表示起始坐标和结束坐标
40 # 第二个参数:表示颜色
41 draw.line((100,100,100,300), fill='red')
42 draw.line((100,100,300,100), fill=(255, 255, 255))
43 5. 画圆
44
45 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
46 draw = ImageDraw.Draw(img, mode='RGB')
47 # 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
48 # 第二个参数:表示开始角度
49 # 第三个参数:表示结束角度
50 # 第四个参数:表示颜色
51 draw.arc((100,100,300,300),0,90,fill="red")
52 6. 写文本
53 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
54 draw = ImageDraw.Draw(img, mode='RGB')
55 # 第一个参数:表示起始坐标
56 # 第二个参数:表示写入内容
57 # 第三个参数:表示颜色
58 draw.text([0,0],'python',"red")
59 7. 特殊字体文字
60
61 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
62 draw = ImageDraw.Draw(img, mode='RGB')
63 # 第一个参数:表示字体文件路径
64 # 第二个参数:表示字体大小
65 font = ImageFont.truetype("kumo.ttf", 28)
66 # 第一个参数:表示起始坐标
67 # 第二个参数:表示写入内容
68 # 第三个参数:表示颜色
69 # 第四个参数:表示颜色
70 draw.text([0, 0], 'python', "red", font=font)
71 图片验证码
72
73 import random
74
75 def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
76 code = []
77 img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
78 draw = ImageDraw.Draw(img, mode='RGB')
79
80 def rndChar():
81 """
82 生成随机字母
83 :return:
84 """
85 return chr(random.randint(65, 90))
86
87 def rndColor():
88 """
89 生成随机颜色
90 :return:
91 """
92 return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
93
94 # 写文字
95 font = ImageFont.truetype(font_file, font_size)
96 for i in range(char_length):
97 char = rndChar()
98 code.append(char)
99 h = random.randint(0, 4)
100 draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
101
102 # 写干扰点
103 for i in range(40):
104 draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
105
106 # 写干扰圆圈
107 for i in range(40):
108 draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
109 x = random.randint(0, width)
110 y = random.randint(0, height)
111 draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
112
113 # 画干扰线
114 for i in range(5):
115 x1 = random.randint(0, width)
116 y1 = random.randint(0, height)
117 x2 = random.randint(0, width)
118 y2 = random.randint(0, height)
119
120 draw.line((x1, y1, x2, y2), fill=rndColor())
121
122 img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
123 return img,''.join(code)
124
125
126 if __name__ == '__main__':
127 # 1. 直接打开
128 # img,code = check_code()
129 # img.show()
130
131 # 2. 写入文件
132 # img,code = check_code()
133 # with open('code.png','wb') as f:
134 # img.save(f,format='png')
135
136 # 3. 写入内存(Python3)
137 # from io import BytesIO
138 # stream = BytesIO()
139 # img.save(stream, 'png')
140 # stream.getvalue()
141
142 # 4. 写入内存(Python2)
143 # import StringIO
144 # stream = StringIO.StringIO()
145 # img.save(stream, 'png')
146 # stream.getvalue()
147
148 pass
149