生成单张验证码图片并显示
from captcha.image import ImageCaptcha import matplotlib.pyplot as plt import numpy as np import random import string #characters为验证码上的字符集,10个数字加26个大写英文字母 #0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ str类型 characters=string.digits+string.ascii_uppercase width,height,n_len,n_class=170,80,4,len(characters) #设置验证码图片的宽度widht和高度height #除此之外还可以设置字体fonts和字体大小font_sizes generator=ImageCaptcha(width=width,height=height) #生成随机的4个字符的字符串 random_str=''.join([random.choice(characters) for j in range(4)]) #生成验证码 img=generator.generate_image(random_str) #显示验证码图片和验证码标题 plt.imshow(img) plt.title(random_str) plt.show()
验证码图片的显示与保存
#显示验证码 img.show() #保存验证码 file_name=random_str+'.jpg' img.save(file_name)
批量生成验证码样本集
from captcha.image import ImageCaptcha import matplotlib.pyplot as plt import numpy as np import random import string from PIL import Image #characters为验证码上的字符集,10个数字加26个大写英文字母 #0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ str类型 characters=string.digits+string.ascii_uppercase width,height,n_len,n_class=170,80,4,len(characters) #生成一万张验证码 for i in range(10000): generator=ImageCaptcha(width=width,height=height) random_str=''.join([random.choice(characters) for j in range(4)]) img=generator.generate_image(random_str) #将图片保存在目录 yangtuo 文件夹下 file_name='./yangtuo/'+random_str+'_'+str(i)+'.jpg' img.save(file_name)
标签:模块,characters,random,验证码,height,captcha,str,import From: https://www.cnblogs.com/shijieli/p/17373579.html