首页 > 其他分享 >利用 captcha 模块完成验证码图片生成

利用 captcha 模块完成验证码图片生成

时间:2023-05-05 11:24:42浏览次数:35  
标签:模块 characters random 验证码 height captcha str import

生成单张验证码图片并显示

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

相关文章

  • 第二节:react-redux详解、分模块、调式工具等
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • [Python]-sklearn模块-机器学习Python入门《Python机器学习手册》-05-处理分类数据
    《Python机器学习手册——从数据预处理到深度学习》这本书类似于工具书或者字典,对于python具体代码的调用和使用场景写的很清楚,感觉虽然是工具书,但是对照着做一遍应该可以对机器学习中python常用的这些库有更深入的理解,在应用中也能更为熟练。以下是根据书上的代码进行实操,注释......
  • Python 模块
    原文:Python模块|菜鸟教程(runoob.com)Python模块(Module),是一个Python文件,以.py结尾,包含了Python对象定义和Python语句。模块让你能够有逻辑地组织你的Python代码段。把相关的代码分配到一个模块里能让你的代码更好用,更易懂。模块能定义函数,类和变量,模块里也能包......
  • 【nodejs基础】模块加载机制及npm指令详解03
    1.模块化的基本概念模块化:是指解决一个复杂问题时,自顶向下逐层把系统划分成若干模块的过程。对于整个系统来说,模块是可组合、分解和更换的单元编程领域中的模块化,就是遵守固定的规则,把一个大文件拆成独立并互相依赖的多个小模块。把代码进行模块化拆分的好处*提高了代码的复......
  • 模块与群体数据3
    #include<iostream>usingnamespacestd;template<classT>voidmySwap(T&x,T&y){ Ttemp=x; x=y; y=temp;}template<classT>voidbubblesort(Ta[],intn){ inti=n-1; while(i>0) { intlastExchangeIndex=0; for(intj=0;j<i;j++) if......
  • 模块与群体数据
    #include<iostream>#include<cassert>usingnamespacestd;template<typenameT>classOrderList{ public: typedefstruct_NODE{ Tvalue; struct_NODE*next; }NODE,*PNODE; OrderList():pHead(NULL),size(0){} OrderList(constOrderList&......
  • [小工具] modinfo - 快速查看内核模块文件信息
    有时候忘了加载模块时参数的名称,可以通过modinfo获取.示例:$modinfo/lib/modules/3.5.0-22-generic/kernel/drivers/media/video/gspca/gspca_mr97310a.kofilename:/lib/modules/3.5.0-22-generic/kernel/drivers/media/video/gspca/gspca_mr97310a.kolicense:......
  • linux模块驱动编写示例
    2023-05-04关键字:该驱动可通过dts配置属性,可在/dev创建节点,支持对/dev下的节点进行标准IO读写以及ioctl读写。dts如下:ir_cut{status="okay";compatible="chorm,ir-cut";gpios=<&gpf0GPIO_ACTIVE_HIGH>,<&gpf1GPIO_ACTIVE_LOW>;}; ......
  • schema模块简介 - 验证数据类型
    目录1schema模块简介2快速上手1.给Schema类传入类型(int、str、float等)2.给Schema类传入可调用的对象(函数、带__call__的类等)3.给Schema类传入带有validate方法的对象4.给Schema类传入容器对象(list、tuple、set等)5.给Schema传入一个字典对象(大部分使用Schema的场景都是传入......
  • 验证码,发送短信验证码,校验确认密码和密码,密码需要数字字母特殊字符任选2种组合
    密码需要数字字母特殊字符任选2种组合constvalidatePwd=(rule,value,callback)=>{constreg=/(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{8,32}$/if(reg.test(value)==true){callback()}else{callback(newError(&#......