Python使用captcha库制作带参数输入验证码案例
作者:SimonLiu009 时间:2022-01-25 20:26:03
最近研究验证码识别,需要生成大量验证码,最方便的是使用captcha
库来生成验证码,网上代码仅仅使用默认设置,但是它还有很多参数可以设定,于是我又添加了一些参数,方便使用。
效果图
参数解析
在上述案例中,完整命令为
python captcha_gen.py w=210 h=100 n=6 c=2 fontsize=[40,50,60]
其中:
w=210
指定验证码图片宽度h=100
指定验证码图片高度n=6
指定验证码字符长度c=2
指定生成的验证码图片数量fontsize=[40,50,60]
指定验证码字体大小(单个字符的字体大小从中随机选取)
参数默认值都写在代码里了。
好像我是唯一一个使用这种方式输入captcha
参数的。
安装相关库
首先安装两个库:
pip install captcha
pip install pillow
完整源码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Use `pip install captcha` and `pip install pillow` to install dependencies
###
from tkinter import font
from captcha.image import ImageCaptcha
from PIL import Image
import random
import time
import os,sys,ast
NUMBER = ['0','1','2','3','4','5','6','7','8','9']
alphabet = ['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']
ALPHABET = ['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']
ALL_CHAR_SET = NUMBER + alphabet + ALPHABET
#最终生成的验证码数量
count = 10
#参数: 宽度、高度、字符长度、字体大小
img_width,img_height,labelno,fontsize = 200,100,4,[42, 50, 56]
def mylist(x):
if isinstance(x,(list,tuple)):
return x
else:
return [x]
def get_argv():
global img_width,img_height,labelno,fontsize,count
if len(sys.argv) > 1:
for i in range(1,len(sys.argv)):
arg = sys.argv[i].split('=')
if arg[0]=='w':
img_width = int(arg[1])
if arg[0]=='h':
img_height = int(arg[1])
if arg[0]=='n':
labelno = int(arg[1])
if arg[0]=='c':
count = int(arg[1])
if arg[0] == 'fontsize':
# fontsize = mylist(int(arg[1]))
fontsize = ast.literal_eval(arg[1])
print(f'img_width:{img_width}, img_height:{img_height}, labelno:{labelno},fonsize:{fontsize},filecount={count}')
def random_captcha_text():
global labelno
captcha_text = []
for i in range(labelno):
c = random.choice(ALL_CHAR_SET)
captcha_text.append(c)
return ''.join(captcha_text)
# 生成字符对应的验证码
def gen_captcha_text_and_image():
global img_width,img_height,fontsize
image = ImageCaptcha(width=img_width,height=img_height,font_sizes=fontsize)
captcha_text = random_captcha_text()
# print('captcha_text:',captcha_text)
captcha_image = Image.open(image.generate(captcha_text))
return captcha_text, captcha_image
def main():
global img_width, img_height, count, labelno
get_argv()
# 保存路径
path = f'{img_width}x{img_height}_{labelno}chars'
print('save path:',path)
if not os.path.exists(path):
os.makedirs(path)
for i in range(count):
now = str(int(time.time()))
text, image = gen_captcha_text_and_image()
filename = text+'_'+now+'.png'
image.save(path + os.path.sep + filename)
print('saved %d : %s' % (i+1,filename))
if __name__ == '__main__':
main()
参考资料:
Python使用captcha制作验证码修改captcha包中image.py文件生成不改变字符形状的有背景和无背景文本验证码
captcha · PyPI
来源:https://blog.csdn.net/toopoo/article/details/124742619
标签:Python,aptcha,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
谈中国站长站的文章干扰码实现方法
2007-10-13 11:13:00
![](https://img.aspxhome.com/file/UploadPic/up/2007101313560193.jpg)
Python简单获取网卡名称及其IP地址的方法【基于psutil模块】
2022-10-07 19:52:15
Python数学建模学习模拟退火算法约束条件处理示例解析
2022-06-16 19:19:20
![](https://img.aspxhome.com/file/2023/3/93593_0s.png)
asp.net生成Excel并导出下载五种实现方法
2023-07-06 11:01:16
![](https://img.aspxhome.com/file/2023/9/77189_0s.jpg)
WEB2.0网页制作标准教程(8)CSS布局入门
2007-09-11 13:21:00
![](https://img.aspxhome.com/file/uploadpic/20079/11/200791113246711.gif)
Python函数式编程之返回函数实例详解
2021-10-31 03:46:26
![](https://img.aspxhome.com/file/2023/2/80402_0s.png)
Python人工智能学习PyTorch实现WGAN示例详解
2022-10-20 18:49:32
![](https://img.aspxhome.com/file/2023/0/95790_0s.png)
Python随机数模块详情
2021-10-26 06:47:34
可以实现在同一页面里的用多按钮进行提交吗?
2009-11-01 18:04:00
基于OpenCV的PHP图像人脸识别技术
2023-11-23 22:02:54
自定义数据库自动编号初始值和步进值
2009-05-19 10:00:00
python 利用turtle模块画出没有角的方格
2022-03-09 04:25:04
![](https://img.aspxhome.com/file/2023/6/72016_0s.jpg)
python如何去除字符串中不想要的字符
2022-01-05 20:37:46
css学习笔记:div在IE6下无法遮盖select
2009-04-30 13:21:00
![](https://img.aspxhome.com/file/UploadPic/20094/30/11f2ae205c6g215-66s.jpg)
ASP.Net MVC 布局页、模板页使用方法详细介绍
2023-06-28 19:24:31
asp实现ACCESS数据库加密方法
2008-04-18 12:33:00
Python中的文本相似度的计算方法总结
2021-02-08 08:40:37
python子类在多继承中使用MRO机制原理
2021-03-09 00:52:35
磁盘缓存专题之一 缓存命中和缓存未命中&缓存与缓冲间的差异
2012-10-07 11:02:46
Python实现自动登录百度空间的方法
2023-11-11 09:11:23