-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
86 lines (77 loc) · 2.26 KB
/
solver.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
from PIL import Image
def checker(img1,img2):
if img1.size[0]!=img2.size[0]:
return False
else:
temp = img1.load()
temp2 = img2.load()
unmatched=0
for i in range(img1.size[0]):
for j in range(img1.size[1]):
if temp[i,j][0]!=temp2[i,j][0]:
unmatched+=1
if unmatched>=10:
return False
return True
def solver(img):
img = Image.open(img)
pix = img.load()
letters = Image.open("data.png")
ledata = letters.load()
for y in range(img.size[1]):
for x in range(img.size[0]):
if (pix[x, y][0] > 75) \
and (pix[x, y][1] > 75) \
and (pix[x, y][2] > 75):
pix[x, y] = (255, 255, 255, 255)
else:
pix[x, y] = (0, 0, 0, 255)
counter = 0
old_x = -1
letterlist = []
for x in range(letters.size[0]):
black = True
for y in range(letters.size[1]):
if ledata[x, y] !=(0,0,0,255):
black = False
break
if black:
if x-old_x>=5:
box = (old_x , 0, x, 40)
letter = letters.crop(box)
letterlist.append(letter)
counter += 1
old_x = x
counter = 0
old_x = -1
caplist = []
for x in range(img.size[0]):
black = True
for y in range(img.size[1]):
if pix[x, y] != (0, 0, 0):
black = False
break
if black:
if x - old_x >= 5:
box = (old_x, 0, x, 40)
letter = img.crop(box)
caplist.append(letter)
counter += 1
old_x = x
alphanum='0123456789abcdefghijklmnopqrstuvwxyz'
captcha=[]
for i in range(len(caplist)):
for j in range(len(letterlist)):
if checker(caplist[i],letterlist[j]):
captcha.append(alphanum[j])
solved=''.join(captcha)
if len(solved)==6:
return solved
else:
raise Exception("Captcha can't be solved.")
if __name__ == '__main__':
for i in range(28):
try:
print(solver(f'ss/cp_{i}.jpg'))
except:
pass