python - 无法从图像中提取单词

标签 python python-3.x web-scraping python-imaging-library python-tesseract

我用 python 结合 pytesseract 编写了一个脚本,用于从图像中提取单词。该图像中只有一个单词 TOOLS 可用,这就是我所追求的。目前,我的以下脚本给出了错误的输出,即 WIS。我该怎么做才能获得文本?

Link to that image

这是我的脚本:

import requests, io, pytesseract
from PIL import Image

response = requests.get('http://facweb.cs.depaul.edu/sgrais/images/Type/Tools.jpg')
img = Image.open(io.BytesIO(response.content))
img = img.resize([100,100], Image.ANTIALIAS)
img = img.convert('L')
img = img.point(lambda x: 0 if x < 170 else 255)
imagetext = pytesseract.image_to_string(img)
print(imagetext)
# img.show()

这是我运行上述脚本时修改图像的状态:

enter image description here

我得到的输出:

WIS

预期输出:

TOOLS

最佳答案

关键是将图像转换与 tesseract 能力相匹配。您的主要问题是字体不是通常的字体。你只需要

from PIL import Image, ImageEnhance, ImageFilter

response = requests.get('http://facweb.cs.depaul.edu/sgrais/images/Type/Tools.jpg')
img = Image.open(io.BytesIO(response.content))

# remove texture
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(0)   # decolorize
img = img.point(lambda x: 0 if x < 250 else 255) # set threshold
img = img.resize([300, 100], Image.LANCZOS) # resize to remove noise
img = img.point(lambda x: 0 if x < 250 else 255) # get rid of remains of noise
# adjust font weight
img = img.filter(ImageFilter.MaxFilter(11)) # lighten the font ;)
imagetext = pytesseract.image_to_string(img)
print(imagetext)

瞧,

TOOLS

被认可。

关于python - 无法从图像中提取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50944106/

相关文章:

python - CouchDB 中的无状态分页?

python - 按元组元素过滤元组列表

python - 如何在 WSGI 中间件中添加 http header ?

python - Python 中的字符串分组

javascript - 无法从下拉框中抓取数据

Python的requests库超时但是得到了浏览器的响应

python - Matlab 的样条曲线在 Python 中等效,三个输入。

python-3.x - 如何将 tf.estimator 转换为 keras 模型?

python-3.x - tkinter 的 askopenfilename 的替代方案

python - 无法解析网页中所有可用的 asin