天天看点

Tensorflow学习:验证码生成

Tensorflow学习:验证码生成

验证码生成库:from captcha.image import ImageCaptcha

随机函数:import random

# 验证码生成库
from captcha.image import ImageCaptcha  # pip install captcha
import numpy as np
from PIL import Image
import random
import sys

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']

# 生成字符的验证码
def random_captcha_text(char_set=number, captcha_size=4):
    # 验证码列表
    captcha_text = []
    for i in range(captcha_size):
        # 随机选择
        c = random.choice(char_set)
        # 加入验证码列表
        captcha_text.append(c)
    return captcha_text


# 生成字符对应的验证码
def gen_captcha_text_and_image():
    image = ImageCaptcha()
    # 获得随机生成的验证码
    captcha_text = random_captcha_text()
    # 把验证码列表转为字符串
    captcha_text = ''.join(captcha_text)
    # 生成验证码
    captcha = image.generate(captcha_text)
    # 写到文件
    image.write(captcha_text, 'captcha/images/' + captcha_text + '.jpg')


# 数量少于10000(0000-9999),因为会重名
num = 10000
if __name__ == '__main__':
    for i in range(num):
        # 生成验证码
        gen_captcha_text_and_image()
        sys.stdout.write('\r>>Creating image %d/%d' % (i + 1, num))
        sys.stdout.flush()
    sys.stdout.write('\n')
    sys.stdout.flush()

    print("生成完毕")

           

运行结果:

Tensorflow学习:验证码生成

生成验证码如下:

Tensorflow学习:验证码生成

继续阅读