-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaptchaidentifier.py
More file actions
107 lines (92 loc) · 3.68 KB
/
captchaidentifier.py
File metadata and controls
107 lines (92 loc) · 3.68 KB
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
from PIL import Image,ImageDraw,ImageEnhance,ImageFilter
from fratures_360buy import FEATURES
class CaptchaIdentifier():
def __init__(self):
pass
def parse(self, img):
#im = img.filter(ImageFilter.CONTOUR).convert('1')
im = img.convert("L")
threshold = 150
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(255)
im = im.point(table, "1")
size = img.size[0]
if img.size[0] == 60:
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15)]]
return '%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==70):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15)]]
return '%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==81):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15),(42,4,51,15)]]
return '%s%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==97):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15),(42,4,51,15),(53,4,62,15)]]
return '%s%s%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==110):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15),(42,4,51,15),(53,4,62,15),(64,4,73,15)]]
return '%s%s%s%s%s' % tuple([self.identify_number(b) for b in blocks])
def parse_newegg(self, img):
im = img.filter(ImageFilter.CONTOUR).convert('1')
#im = img.convert("L")
threshold = 150
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(255)
im = im.point(table, "1")
size = img.size[0]
if img.size[0] == 60:
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15)]]
return '%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==75):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15),(15,4,51,15)]]
return '%s%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==100):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15), (47,4,56,15), (58,4,67,15)]]
return '%s%s%s%s' % tuple([self.identify_number(b) for b in blocks])
def parse_suning(self, img):
im = img.filter(ImageFilter.CONTOUR).convert('1')
#im = img.convert("L")
threshold = 150
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(255)
im = im.point(table, "1")
size = img.size[0]
if img.size[0] == 60:
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15)]]
return '%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==75):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15),(15,4,51,15)]]
return '%s%s%s' % tuple([self.identify_number(b) for b in blocks])
elif (img.size[0]==100):
blocks = [list(im.crop(b).getdata()) for b in [(20,4,29,15),(31,4,40,15), (47,4,56,15), (58,4,67,15)]]
return '%s%s%s%s' % tuple([self.identify_number(b) for b in blocks])
def list_distance(self, m, n):
len_plus = lambda x: len(x) + 1
c = [[i] for i in range(0, len_plus(m))]
c[0] = [j for j in range(0, len_plus(n))]
for i in range(0, len(m)):
for j in range(0, len(n)):
c[i+1].append(
min(
c[i][j+1] + 1,
c[i+1][j] + 1,
c[i][j] + (0 if m[i] == n[j] else 1)
)
)
return c[-1][-1]
def identify_number(self, source):
distance = [self.list_distance(source, i) for i in FEATURES]
minimal = min(distance)
return distance.index(minimal)