forked from dee1024/pytorch-captcha-recognition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha_gen.py
32 lines (28 loc) · 961 Bytes
/
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
# -*- 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 = 5
path = captcha_setting.PREDICT_DATASET_PATH+'/1'
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))