-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-image.py
70 lines (55 loc) · 2.37 KB
/
generate-image.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import os
import numpy
import random
import string
import cv2
import argparse
import captcha.image
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--width', help='Width of captcha image', type=int)
parser.add_argument('--height', help='Height of captcha image', type=int)
parser.add_argument('--length', help='Length of captchas in characters', type=int)
parser.add_argument('--count', help='How many captchas to generate', type=int)
parser.add_argument('--output-dir', help='Where to store the generated captchas', type=str)
parser.add_argument('--symbols', help='File with the symbols to use in captchas', type=str)
args = parser.parse_args()
if args.width is None:
print("Please specify the captcha image width")
exit(1)
if args.height is None:
print("Please specify the captcha image height")
exit(1)
if args.length is None:
print("Please specify the captcha length")
exit(1)
if args.count is None:
print("Please specify the captcha count to generate")
exit(1)
if args.output_dir is None:
print("Please specify the captcha output directory")
exit(1)
if args.symbols is None:
print("Please specify the captcha symbols file")
exit(1)
captcha_generator = captcha.image.ImageCaptcha(width=args.width, height=args.height)
symbols_file = open(args.symbols, 'r')
captcha_symbols = symbols_file.readline().strip()
symbols_file.close()
print("Generating captchas with symbol set {" + captcha_symbols + "}")
if not os.path.exists(args.output_dir):
print("Creating output directory " + args.output_dir)
os.makedirs(args.output_dir)
for i in range(args.count):
captcha_text = ''.join([random.choice(captcha_symbols) for j in range(args.length)])
image_path = os.path.join(args.output_dir, captcha_text+'.png')
if os.path.exists(image_path):
version = 1
while os.path.exists(os.path.join(args.output_dir, captcha_text + '_' + str(version) + '.png')):
version += 1
image_path = os.path.join(args.output_dir, captcha_text + '_' + str(version) + '.png')
image = numpy.array(captcha_generator.generate_image(captcha_text))
cv2.imwrite(image_path, image)
if __name__ == '__main__':
main()