-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (32 loc) · 1.18 KB
/
main.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
from PIL import ImageFont, ImageDraw, Image
from random import random, choices, randint
import numpy as np
import cv2
import os
import sys
from string import ascii_uppercase # , ascii_lowercase, digits
myfonts = [ImageFont.truetype(font="./fonts/"+i, size=35)
for i in os.listdir("./fonts/")]
os.system("mkdir -p data")
n = int(sys.argv[1]) # Total captchas needs to be generated
for _ in range(n):
img = np.zeros(shape=(70, 185, 3), dtype=np.uint8)
img_raw = Image.fromarray(img+255)
draw = ImageDraw.Draw(img_raw)
font = myfonts[randint(0, len(myfonts)-1)]
my_cap = ''.join(choices(ascii_uppercase, k=6))
for i in range(6):
draw.text((5+5*i+25*i, 10), my_cap[i], font=font, fill=(0, 0, 0))
img = cv2.GaussianBlur(img,(5,5),0)
img = np.array(img_raw)
# # Adding noise
# thresh = 0.02
# for i in range(img.shape[0]):
# for j in range(img.shape[1]):
# rdn = random()
# if rdn < thresh:
# img[i][j] = 0
# elif rdn > 1-thresh:
# img[i][j] = 255
cv2.imwrite("./data/"+my_cap+".png", img)
print(n, "captchas generated using", len(myfonts), "fonts at ./data/")