def updateText(self):
self._pixmap = QPixmap('TEXT.png')
size = QSize(160, 90)
pix = self._pixmap.scaled(size,
transformMode=Qt.SmoothTransformation)
self._lbl.setPixmap(pix)
self._image = Image.open('TEXT.png')
text = pytesseract.image_to_string(self._image, lang='eng', config='-psm 8', )
self._line.setText(text)
self.signal_send_text.emit(text)
python类image_to_string()的实例源码
def img_to_string(self):
"""
???????
:return:
"""
# ????
self.crop_img()
# ????
self.optimize_img()
# ????
self.img_text = pytesseract.image_to_string(self.img_fp)
# ????
print '??????%s' % self.img_text
self.optimize_text()
print '??????%s' % self.img_text
def test2(image_name):
"""
?????????????
:param image_name:
:return:
"""
with Image.open(image_name) as image:
image = image.convert("RGBA")
pixdata = image.load()
# Make the letters bolder for easier recognition
for y in range(image.size[1]):
for x in range(image.size[0]):
if pixdata[x, y][0] < 90:
pixdata[x, y] = (0, 0, 0, 255)
for y in range(image.size[1]):
for x in range(image.size[0]):
if pixdata[x, y][1] < 136:
pixdata[x, y] = (0, 0, 0, 255)
for y in range(image.size[1]):
for x in range(image.size[0]):
if pixdata[x, y][2] > 0:
pixdata[x, y] = (255, 255, 255, 255)
# image.save("input-black.gif", "GIF")
print(pytesseract.image_to_string(image))
def image_to_string(image):
"""
?????????? 4 ???
:param image:
:return:
"""
global font
test1 = convert_black_white(image)
text = str()
for each in cut(test1):
for num in range(10):
if create_pix_tables(each) == font[num]:
text += str(num)
break
return text
def solve():
"""
?? WP ??, ??????????????????, ?????????
:return:
"""
global font
font = get_font()
path = "/Users/L1n/Desktop/bmp"
sum = 0
for i in range(1, 10000):
with Image.open(path + os.sep + str(i) + ".bmp") as image:
sum += i * int(image_to_string(image))
print("Sum: {}".format(sum))
def recognizeImage(results, cvimage ,rect, language, charWhiteList=None):
config = "-psm 7" # single line mode
if charWhiteList is not None:
config += " -c tessedit_char_whitelist=" + charWhiteList
image = Image.fromarray(cvimage)
result = pytesseract.image_to_string(image, lang=language, config=config)
item = ImageRecognizerItem(result, rect)
results.append(item)
# ??ImageRecognizerItem
def recognize_url(url):
import urllib.request
urllib.request.urlretrieve(url, './img.jpg')
img = Image.open('./img.jpg')
img = img.convert('RGBA')
w, h = img.size[0], img.size[1]
point_list = gen_white_black_points(img)
print_char_pic(w, h, point_list)
reduce_noisy(w, h, point_list)
print_char_pic(w, h, point_list)
img.putdata(point_list)
img.save("C:\\Users\\poluo\\PycharmProjects\\douban\\douban\\processed.jpg")
tmp=Image.open('C:\\Users\\poluo\\PycharmProjects\\douban\\douban\\processed.jpg')
return pytesseract.image_to_string(tmp)
def solution_from_image(image):
pieces = filter_split(image)
if len(pieces) != 4:
return '????'
string = ''
for piece in pieces:
try:
solved = pytesseract.image_to_string(piece, config='-psm 10 -c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyz')
except pytesseract.pytesseract.TesseractError:
solved = None
if not solved:
solved = '?'
string += solved
return string
def parse_captcha(self, link, status):
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
proxy = urllib2.ProxyHandler({'http': 'http://14.142.4.33'})
opener = urllib2.build_opener(proxy)
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def parse_captcha(self, link, status):
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
proxy = urllib2.ProxyHandler({'http': 'http://14.142.4.33'})
opener = urllib2.build_opener(proxy)
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def parse_captcha(self, link, status):
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
proxy = urllib2.ProxyHandler({'http': 'http://14.142.4.33'})
opener = urllib2.build_opener()
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def parse_captcha(self, link, status):
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
proxy = urllib2.ProxyHandler({'http': 'http://14.142.4.33'})
opener = urllib2.build_opener()
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def parse_captcha(self, link, status):
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
proxy = urllib2.ProxyHandler({'http': 'http://14.142.4.33'})
opener = urllib2.build_opener()
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def parse_captcha(self, link, status):
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
proxy = urllib2.ProxyHandler({'http': 'http://14.142.4.33'})
opener = urllib2.build_opener()
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def parse_captcha(self, link, status):
print "\n\n status in captcha : ", status
print "\n link in captcha : ", link
try:
if status == 0:
#proxies = ['http://43.242.104.43', 'http://115.113.43.215', 'http://115.113.43.215']
#proxy = random.choice(proxies)
#proxy = urllib2.ProxyHandler({'http': 'http://115.113.43.215'})
opener = urllib2.build_opener()
header = ua.random
print "\n header : ", header
print "\n link : ", link
opener.addheaders = [('User-agent', header)]
data = opener.open(link).read()
soup = BeautifulSoup(data, 'html.parser')
div1 = soup.find("div", {"class": "a-row a-text-center"})
if div1 is not None:
img = div1.find("img")
image = img["src"]
print "\n captcha.."
print "image : ", image
image = Image.open(StringIO(requests.get(image).content))
image.filter(ImageFilter.SHARPEN)
captcha = pytesseract.image_to_string(image)
print "captcha : ", captcha
values = {'field-keywords' : captcha}
data = urllib.urlencode(values)
req = urllib2.Request(link, data, {'User-agent': header})
resp = urllib2.urlopen(req)
the_page = resp.read()
self.parse_captcha(link, status)
else:
status = 1
return
except Exception as e:
print "\n Exception : ", e
def ocr(img):
bw = img_to_bw(img)
captcha = pytesseract.image_to_string(bw)
cleaned = ''.join(c for c in captcha.lower() if c in string.ascii_lowercase)
if len(cleaned) != len(captcha):
print('removed bad characters: {}'.format(set(captcha) - set(cleaned)))
return cleaned
def cmd_info(message, parameters, recursion=0):
async for msg in client.logs_from(message.channel, limit=25):
try:
if msg.attachments:
image = Image.open(BytesIO(requests.get(msg.attachments[0]['url']).content)).filter(ImageFilter.SHARPEN)
text = pytesseract.image_to_string(image)
if not text:
e = discord.Embed(colour=0xB5434E)
e.description = "I just forgot how to read..."
else:
e = discord.Embed(colour=0x43B581)
e.description = text
await client.send_message(message.channel, embed=e)
return
except OSError:
e = discord.Embed(colour=0xB5434E)
e.description = "Image way big, are you trying to kill me?"
await client.send_message(message.channel, embed=e)
return
except TypeError:
e = discord.Embed(colour=0xB5434E)
e.description = "Latest attachment is not a static image, try again."
await client.send_message(message.channel, embed=e)
return
except:
e = discord.Embed(colour=0xB5434E)
e.description = "Error ocurred, not related to OSError or TypeError I guess."
await client.send_message(message.channel, embed=e)
return
e = discord.Embed(colour=0xB5434E)
e.description = "I can't find an image in the last 25 posts, that or I'm retarded."
await client.send_message(message.channel, embed=e)
def get_vcode(path):
with Image.open(path) as image:
mutex.acquire(1)
vcode = pytesseract.image_to_string(image, lang='numfont')
mutex.release()
return vcode.replace(',', '').replace('\n', '')
def get_vcode_by_img_0(img):
mutex.acquire(1)
vcode = pytesseract.image_to_string(img, lang='numfont')
if vcode == '':
img = merge_thumb_0(img)
vcode = pytesseract.image_to_string(img, lang='numfont')
if vcode == '00':
vcode = '0'
else:
vcode = vcode.strip('0')
mutex.release()
return vcode.replace(',', '').replace('\n', '')