-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwork_vcode.py
133 lines (114 loc) · 5.13 KB
/
work_vcode.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from PIL import Image, ImageDraw, ImageFont
from random import randint
import os
import sys
import platform
if 'Windows' in platform.platform():
PATH = "\\".join( os.path.abspath(__file__).split('\\')[:-1])
FONTPATH = ["{}\\Times Bold.ttf".format(PATH),
"{}\\Courier-BoldRegular.ttf".format(PATH)]
else:
PATH = "/".join( os.path.abspath(__file__).split('/')[:-1])
FONTPATH = ["{}/Times Bold.ttf".format(PATH),
"{}/Courier-BoldRegular.ttf".format(PATH)]
sys.path.append(PATH)
#FONTPATH = ["Times Bold.ttf"]
import random
class rect:
def __init__(self):
self.size = (randint(5, 21), randint(5, 21))
self.location = (randint(1, 199), randint(1, 59))
self.luoverlay = True if randint(1, 10) > 6 else False
self.rdoverlay = False if self.luoverlay else True if randint(1, 10) > 8 else False
self.lucolor = 0 if randint(0, 1) else 255
self.rdcolor = 0 if self.lucolor == 255 else 255
self.ludrawn = False
self.rddrawn = False
self.pattern = randint(0, 1)
def draw(self, image, overlay):
if((overlay or not self.luoverlay) and not self.ludrawn):
self.ludrawn = True
stp = self.location
transparent = int(255 * 0.45 if self.lucolor == 0 else 255 * 0.8)
color = (self.lucolor, self.lucolor, self.lucolor, transparent)
uline = Image.new("RGBA", (self.size[0], 1), color)
lline = Image.new("RGBA", (1, self.size[1]), color)
image.paste(uline, stp, uline)
image.paste(lline, stp, lline)
if((overlay or not self.rdoverlay) and not self.rddrawn):
self.rddrawn = True
dstp = (self.location[0], self.location[1] + self.size[1])
rstp = (self.location[0] + self.size[0], self.location[1])
transparent = int(255 * 0.45 if self.rdcolor == 0 else 255 * 0.8)
color = (self.rdcolor, self.rdcolor, self.rdcolor, transparent)
dline = Image.new("RGBA", (self.size[0], 1), color)
rline = Image.new("RGBA", (1, self.size[1]), color)
image.paste(dline, dstp, dline)
image.paste(rline, rstp, rline)
A_Za_z = []
for i in range(65, 91):
A_Za_z.append( chr(i) )
for i in range(10):
A_Za_z.append(i)
# self = captchatext(1,0)
class captchatext:# priority = 1; offset = 0
def __init__(self, priority, offset):
self.number = random.sample(A_Za_z,1)[0]
#self.number = randint(1,10)
self.color = [randint(10, 140) for _ in range(3)]
self.angle = randint(-55, 55)
self.priority = priority
self.offset = 0
self.next_offset = 0
def draw(self, image):
fontpath = FONTPATH[ random.sample(range(2),1)[0] ]
color = (self.color[0], self.color[1], self.color[2], 255)
font = ImageFont.truetype( fontpath , randint(25, 27) * 10)
text = Image.new("RGBA", (250, 300), (0, 0, 0, 0))
textdraw = ImageDraw.Draw(text)
textdraw.text((0, 0), str(self.number), font=font, fill=color)
#textdraw.text((0, 0), 'j', font=font, fill=color)
text = text.rotate(self.angle, expand=True)
text = text.resize((int(text.size[0] / 10), int(text.size[1] / 10)))
base = int(self.priority * (200 / 6))
rand_min = (self.offset - base - 2) if (self.offset - base - 2) >= -15 else -15
rand_min = 0 if self.priority == 0 else rand_min
rand_max = (33 - text.size[0]) if self.priority == 5 else (33 - text.size[0] + 10)
try:
displace = randint(rand_min, rand_max)
except:
displace = rand_max
location = (base + displace, randint(3, 23))
self.next_offset = location[0] + text.size[0]
image.paste(text, location, text)
# plt.imshow(image)
def work_vcode_fun(amount,file_path,amount2):# amount = 5 ; file_path = 'test_data'
os.chdir(PATH)
if file_path not in os.listdir():
os.makedirs(file_path)
#numberlist = []
#status = 1
for index in range(amount):
if index % 100==0: print(index)
#print(index)
# index = 1
numberstr = ""
bgcolor = [randint(180, 250) for _ in range(3)]
captcha = Image.new('RGBA', (200, 60), (bgcolor[0], bgcolor[1], bgcolor[2], 255))
rectlist = [rect() for _ in range(32)]
for obj in rectlist:
obj.draw(image=captcha, overlay=False)
offset = 0
#vcode = ''
#amount2 = random.sample([5,6],1)[0]
for i in range(amount2):
newtext = captchatext(i, offset)
newtext.draw(image=captcha)
offset = newtext.next_offset
numberstr += str(newtext.number)
if 'Windows' in platform.platform():
path = '{}\\{}\\{}.jpg'.format(PATH,file_path,numberstr)
captcha.convert("RGB").save(path, "JPEG")
else:
path = '{}/{}/{}.jpg'.format(PATH,file_path,numberstr)
captcha.convert("RGB").save(path, "JPEG")