-
Notifications
You must be signed in to change notification settings - Fork 17
/
run_demo.py
85 lines (70 loc) · 2.96 KB
/
run_demo.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from multicolorcaptcha import CaptchaGenerator
from os import path, makedirs
# Path to generate Captchas
GEN_CAPTCHAS_FOLDER = "./captchas"
# Captcha image size number (2 -> 640x360)
CAPCTHA_SIZE_NUM = 2
def gen_captchas(CaptchaGen: CaptchaGenerator,
num_captchas: int, out_img_dir: str) -> None:
"""Generate num_captchas captchas and store in out_img_dir directory.
Parameters
----------
CaptchaGen : CaptchaGenerator
num_captchas : int
out_img_dir : str
"""
for i in range(0, num_captchas):
# Use one of the following captcha generation options
# captcha = CaptchaGen.gen_captcha_image()
# captcha = CaptchaGen.gen_captcha_image(
# multicolor=False, margin=False
# )
# captcha = CaptchaGen.gen_captcha_image(multicolor=True, margin=False)
# captcha = CaptchaGen.gen_captcha_image(multicolor=True, margin=True)
captcha = CaptchaGen.gen_captcha_image(difficult_level=2)
# captcha = CaptchaGen.gen_captcha_image(difficult_level=4)
# captcha = CaptchaGen.gen_captcha_image(chars_mode="hex")
# captcha = CaptchaGen.gen_captcha_image(chars_mode="ascii")
# captcha = CaptchaGen.gen_captcha_image(
# difficult_level=5, multicolor=True, chars_mode="ascii"
# )
image = captcha["image"]
characters = captcha["characters"]
print("Generated captcha {}: {}".format(i + 1, characters))
image.save("{}/{}.png".format(out_img_dir, i+1), "png")
def gen_math_captchas(CaptchaGen: CaptchaGenerator,
num_captchas: int, out_img_dir: str):
"""Generate num_captchas math captchas and store in out_img_dir directory.
Parameters
----------
CaptchaGen : CaptchaGenerator
num_captchas : int
out_img_dir : str
"""
for i in range(0, num_captchas):
# Use one of the following captcha generation options
captcha = CaptchaGen.gen_math_captcha_image(2)
# captcha = CaptchaGen.gen_math_captcha_image(2, multicolor=True)
image = captcha["image"]
equation_str = captcha["equation_str"]
equation_result = captcha["equation_result"]
print("Generated captcha {}: {} = {}".format(
str(i+1), equation_str, equation_result)
)
image.save("{}/{}.png".format(out_img_dir, str(i+1)), "png")
def demo() -> None:
# Create Captcha Generator object of specified size
CaptchaGen = CaptchaGenerator(CAPCTHA_SIZE_NUM)
# If it doesn't exists, create captchas folder to store generated captchas
if not path.exists(GEN_CAPTCHAS_FOLDER):
makedirs(GEN_CAPTCHAS_FOLDER)
# Generate 20 captchas
gen_captchas(CaptchaGen, 20, GEN_CAPTCHAS_FOLDER)
# gen_math_captchas(CaptchaGen, 20, GEN_CAPTCHAS_FOLDER)
print("Process completed. Check captchas images at \"{}\" folder.".format(
GEN_CAPTCHAS_FOLDER
))
if __name__ == '__main__':
demo()