-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha_gen.py
50 lines (39 loc) · 1.63 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from PIL import ImageFont, Image, ImageDraw
import random
import string
import os
class Captcha:
def __init__(self):
self.key = self.random_key()
self.font = ImageFont.truetype("MotivaSans-Thin.otf", 23)
self.size = (120, 35)
self.background = (245, 245, 245)
self.circle_color = (180, 180, 180)
self.rotate = [angle for angle in range(-2, 2) if angle != 0]
self.x_off = 10
self.y_off = 3
self.char_color = [(150, 150, 150), (92, 92, 92)]
def random_key(self):
key = ''
for letter in range(6):
key += random.choice(string.ascii_lowercase + string.digits)
return key
def create_image(self):
image1 = Image.new("RGB", self.size, self.background)
draw = ImageDraw.Draw(image1)
for i in range(4):
x, y, r = (random.randint(0, 120), random.randint(0, 35), random.randint(3, 20))
draw.ellipse((x - r, y - r, x + r, y + r), outline=self.circle_color)
for char in self.key:
image2 = Image.new("RGBA", self.size)
draw2 = ImageDraw.Draw(image2)
draw2.text((self.x_off, self.y_off), text=char, font=self.font, fill=random.choice(self.char_color))
image2 = image2.rotate(random.choice(self.rotate), expand=1)
image1.paste(image2, (0, 0), image2)
self.x_off += 16
path = "./data/"
if os.path.exists(path) is False:
os.mkdir(path)
image1.save(path + self.key + ".png")
if __name__ == "__main__":
[Captcha().create_image() for x in range(100)] # number of captchas to be created