机器视觉(4)--生成验证码
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()
测试结果: