python 生成验证码

from captcha.image import ImageCaptcha #验证码包
import numpy as np
import random
import sys


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

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_immage():
    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')
    
num = 13000 #结果小于10000  结果由重复
if __name__ =='__main__':
    for i in range(num):
        gen_captcha_text_and_immage()
        sys.stdout.write('\r>>Creating image %d/%d' % (i+1,num))
        sys.stdout.flush()
    sys.stdout.write('\n')
    sys.stdout.flush()
    
    print("生成完毕")

你可能感兴趣的:(python 生成验证码)