欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

机器视觉(4)--生成验证码

程序员文章站 2022-05-11 08:05:01
python生成随机验证码 用python生成验证码,为后期训练自己的模型做准备 import numpy as py from captcha.image import ImageCaptcha import os number = ['1', '2', '3', '4', '5', '6', ' ......

python生成随机验证码

用python生成验证码,为后期训练自己的模型做准备

import numpy as py
from captcha.image import imagecaptcha
import  os

number = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

def random_caption_text(char_set=number, captcha_size=4):
    captcha_text = []
    for i in range(captcha_size):
        # 随机选择
        c = py.random.choice(char_set)
        captcha_text.append(c)
    return captcha_text


def get_captcha_text_to_image():
    image = imagecaptcha()
    # 随机获取验证码
    random_num = random_caption_text()
    #     把验证码转换为字符串
    captcha_text = ''.join(random_num)
    #     生成验证码
    captcha = image.generate(captcha_text)
    image.write(captcha_text, 'c:/users/administrator/desktop/image/captcha/' + captcha_text + '.jpg')

def main():

    for i in range(1000):
        get_captcha_text_to_image()



if __name__ == '__main__':
    main()

测试结果:

机器视觉(4)--生成验证码