forked from dee1024/pytorch-captcha-recognition
-
Notifications
You must be signed in to change notification settings - Fork 6
/
captcha_gen.py
34 lines (30 loc) · 1.07 KB
/
captcha_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: UTF-8 -*-
from captcha.image import ImageCaptcha # pip install captcha
from PIL import Image
import random
import time
import captcha_setting
import os
def random_captcha():
captcha_text = []
for i in range(captcha_setting.MAX_CAPTCHA):
c = random.choice(captcha_setting.ALL_CHAR_SET)
captcha_text.append(c)
return ''.join(captcha_text)
# 生成字符对应的验证码
def gen_captcha_text_and_image():
image = ImageCaptcha()
captcha_text = random_captcha()
captcha_image = Image.open(image.generate(captcha_text))
return captcha_text, captcha_image
if __name__ == '__main__':
count = 30
path = captcha_setting.TRAIN_DATASET_PATH #通过改变此处目录,以生成 训练、测试和预测用的验证码集
if not os.path.exists(path):
os.makedirs(path)
for i in range(count):
now = str(int(time.time()))
text, image = gen_captcha_text_and_image()
filename = text+'_'+now+'.png'
image.save(path + os.path.sep + filename)
print('saved %d : %s' % (i+1,filename))