我用 python
结合 pytesseract
编写了一个脚本,用于从图像中提取单词。该图像中只有一个单词 TOOLS
可用,这就是我所追求的。目前,我的以下脚本给出了错误的输出,即 WIS
。我该怎么做才能获得文本?
这是我的脚本:
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()
这是我运行上述脚本时修改图像的状态:
我得到的输出:
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/