You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hunjianghu/gzy/tesorflow/生成验证码.py

39 lines
1.1 KiB

6 years ago
import os
import time
import random
import shutil
from captcha.image import ImageCaptcha
CHAR_SET = ['0','1','2','3','4','5','6','7','8','9']
CHAR_SET_LEN = 10
CAPTCHA_LEN = 4
CAPTCHA_IMAGE_PATH = 'D:/tensorflow/captcha/images/'
TEST_IMAGE_PATH = 'D:/tensorflow/captcha/test/'
TEST_CASE = 200
def generate_captcha_image(PATH):
image = ImageCaptcha()
for i in range(10):
for j in range(10):
for k in range(10):
for z in range(10):
cap_text = CHAR_SET[i]+CHAR_SET[j]+CHAR_SET[k]+CHAR_SET[z]
image.write(cap_text,PATH+cap_text+'.jpg')
print("Finish Write!")
def generate_test_image():
fileNameList = []
for filePath in os.listdir(CAPTCHA_IMAGE_PATH):
captcha_name = filePath.split('/')[-1]
fileNameList.append(captcha_name)
random.seed(time.time())
random.shuffle(fileNameList)
for i in range(TEST_CASE):
name = fileNameList[i]
shutil.move(CAPTCHA_IMAGE_PATH + name, TEST_IMAGE_PATH + name)
if __name__ == '__main__':
generate_captcha_image(CAPTCHA_IMAGE_PATH)
generate_test_image()