[python每日一练]--0010:生成验证码图片

题目链接:https://github.com/Show-Me-the-Code/show-me-the-code
代码github链接:https://github.com/wjsaya/python_spider_learn/tree/master/python_daily
个人博客地址:https://wjsaya.github.io
第 0010 题: 使用 Python 生成类似于下图中的字母验证码图片

效果图

思路:

  1. 根据指定位数获取随机验证码字符串:直接用random模块即可。
  2. 把字符串转换成图片:通过PIL库画图。

代码:

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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: wjsaya(http://www.wjsaya.top)
# @Date: 2018-08-10 00:01:32
# @Last Modified by: wjsaya(http://www.wjsaya.top)
# @Last Modified time: 2018-08-10 00:46:47
import random
import string
from PIL import Image, ImageFont, ImageDraw, ImageFilter
def get_str():
'''获取单个随机字符
string.digits + string.ascii_letters =
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
'''
return random.choice(string.digits + string.ascii_letters)
def get_color():
'''返回颜色元组
'''
return (random.randint(64,200),random.randint(64,200),random.randint(64,200))
def get_pic(num=4):
'''生成指定验证码个数的验证码图片,默认为4个,每个的大小均为60*60
'''
heigh = 60
width = heigh * num
image = Image.new('RGB', (width, heigh), (255, 255, 255))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('ariblk.ttf',44)
# 创建图片,画布,以及字体对象
for x in range(width):
for y in range(heigh):
draw.point((x,y),fill=get_color())
# 画布随机加噪点
for t in range(num):
draw.text((60 * t + 10, 0), get_str(), font=font, fill=get_color())
# 随机获取num个字符,使用指定字体,使用随机颜色
image = image.filter(ImageFilter.SMOOTH)
# 模糊处理图片
image.save('vercode.png')
if __name__ == '__main__':
num = 4
get_pic(num)

效果图:

0010

0%