python之快捷生成图像验证码
python快捷生成图像验证码
使用captcha库
pip install captcha -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
示例
from captcha.image import ImageCaptcha
img = ImageCaptcha().generate_image("abchef") # 想要生成的验证码abchef
img.show() # 展示生成的验证码
当然你也可以指定范围,随机生成二维码
from captcha.image import ImageCaptcha
from random import randint
list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
data = ''
for i in range(6): # 可指定验证码的位数,此处为6位
data += list[randint(0,62)] # 此处62代表list中的元素个数
img= ImageCaptcha().generate_image(data)
img.show()