Repository navigation
Expand file tree
/
Copy pathLinksaveIn.py
More file actions
160 lines (130 loc) · 4.48 KB
/
Copy pathLinksaveIn.py
File metadata and controls
160 lines (130 loc) · 4.48 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
try:
from PIL import Image
except ImportError:
import Image
import glob
import os
from module.plugins.internal.OCR import OCR
class LinksaveIn(OCR):
__name__ = "LinksaveIn"
__type__ = "ocr"
__version__ = "0.17"
__status__ = "testing"
__description__ = """Linksave.in ocr plugin"""
__license__ = "GPLv3"
__authors__ = [("pyLoad Team", "admin@pyload.org")]
def init(self):
self.data_dir = os.path.dirname(os.path.abspath(__file__)) + os.sep + "LinksaveIn" + os.sep
def load_image(self, image):
im = Image.open(image)
frame_nr = 0
lut = im.resize((256, 1))
lut.putdata(xrange(256))
lut = list(lut.convert("RGB").getdata())
new = Image.new("RGB", im.size)
npix = new.load()
while True:
try:
im.seek(frame_nr)
except EOFError:
break
frame = im.copy()
pix = frame.load()
for x in xrange(frame.size[0]):
for y in xrange(frame.size[1]):
if lut[pix[x, y]] != (0, 0, 0):
npix[x, y] = lut[pix[x, y]]
frame_nr += 1
new.save(self.data_dir+"unblacked.png")
self.img = new.copy()
self.pixels = self.img.load()
self.result_captcha = ""
def get_bg(self):
stat = {}
cstat = {}
img = self.img.convert("P")
for bgpath in glob.glob(self.data_dir+"bg/*.gif"):
stat[bgpath] = 0
bg = Image.open(bgpath)
bglut = bg.resize((256, 1))
bglut.putdata(xrange(256))
bglut = list(bglut.convert("RGB").getdata())
lut = img.resize((256, 1))
lut.putdata(xrange(256))
lut = list(lut.convert("RGB").getdata())
bgpix = bg.load()
pix = img.load()
for x in xrange(bg.size[0]):
for y in xrange(bg.size[1]):
rgb_bg = bglut[bgpix[x, y]]
rgb_c = lut[pix[x, y]]
try:
cstat[rgb_c] += 1
except Exception:
cstat[rgb_c] = 1
if rgb_bg == rgb_c:
stat[bgpath] += 1
max_p = 0
bg = ""
for bgpath, value in stat.items():
if max_p < value:
bg = bgpath
max_p = value
return bg
def substract_bg(self, bgpath):
bg = Image.open(bgpath)
img = self.img.convert("P")
bglut = bg.resize((256, 1))
bglut.putdata(xrange(256))
bglut = list(bglut.convert("RGB").getdata())
lut = img.resize((256, 1))
lut.putdata(xrange(256))
lut = list(lut.convert("RGB").getdata())
bgpix = bg.load()
pix = img.load()
orgpix = self.img.load()
for x in xrange(bg.size[0]):
for y in xrange(bg.size[1]):
rgb_bg = bglut[bgpix[x, y]]
rgb_c = lut[pix[x, y]]
if rgb_c == rgb_bg:
orgpix[x, y] = (255, 255, 255)
def eval_black_white(self):
new = Image.new("RGB", (140, 75))
pix = new.load()
orgpix = self.img.load()
thresh = 4
for x in xrange(new.size[0]):
for y in xrange(new.size[1]):
rgb = orgpix[x, y]
r, g, b = rgb
pix[x, y] = (255, 255, 255)
if r > max(b, g)+thresh:
pix[x, y] = (0, 0, 0)
if g < min(r, b):
pix[x, y] = (0, 0, 0)
if g > max(r, b)+thresh:
pix[x, y] = (0, 0, 0)
if b > max(r, g)+thresh:
pix[x, y] = (0, 0, 0)
self.img = new
self.pixels = self.img.load()
def recognize(self, image):
self.load_image(image)
bg = self.get_bg()
self.substract_bg(bg)
self.eval_black_white()
self.to_greyscale()
self.img.save(self.data_dir+"cleaned_pass1.png")
self.clean(4)
self.clean(4)
self.img.save(self.data_dir+"cleaned_pass2.png")
letters = self.split_captcha_letters()
final = ""
for n, letter in enumerate(letters):
self.img = letter
self.img.save(ocr.data_dir+"letter%d.png" % n)
self.run_tesser(True, True, False, False)
final += self.result_captcha
return final