Python使用captcha库制作验证码,带参数输入

Python使用captcha库制作验证码,带参数输入,第1张

文章目录
  • 效果图
  • 参数解析
  • 安装相关库
  • 完整源码

最近研究验证码识别,需要生成大量验证码,最方便的是使用 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参数的。

其他例子:

python captcha_gen.py w=210 h=100 n=4 c=100
python captcha_gen.py n=6 c=100
python captcha_gen.py
安装相关库

首先安装两个库:

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()

参考资料:

  1. Python使用captcha制作验证码
  2. 修改captcha包中image.py文件生成不改变字符形状的有背景和无背景文本验证码
  3. captcha · PyPI

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/917127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存