Tesseract无法识别白色紫色文字的图像,甚至转换为B& W.

时间:2018-04-28 03:22:44

标签: python tesseract

我正在使用下面的代码。但是,这个简单的图像中,tesseract甚至都无法使用。

from PIL import Image
import pytesseract
image_file = Image.open("question.png")

image_file = image_file.convert('1') # convert image to black and white
image_file.save('question.png')
text = pytesseract.image_to_string(image_file, lang = 'eng',config='-psm 6')
print(text)

注意: [1.]试图将Pic变成黑白 [2.]使用过psm

问题:如何正确地对给定图像进行OCR处理?

Question.png

更新

我现在使用了这段代码:

from PIL import Image
import pytesseract

image_file = Image.open('purple.png')

image_file = image_file.convert('L') # convert image to black and white
image_file.save('question.png')
image_file = Image.open('question.png') #without this line also the output is same
text = pytesseract.image_to_string(image_file, lang = 'eng',config='-psm 6')
print(text)

输出:

enter image description here

请帮助我为什么它不起作用。

1 个答案:

答案 0 :(得分:3)

使用image.convert('-1')即可获得此图片

enter image description here

这是一张带有很多噪音的照片,所以Tesseract会失败。

我建议您使用image_file.convert('L')将其转换为更好的灰度图像(有关'1'模式和'L'模式的详细信息,请查看documentation)。使用'L'模式,您将获得

enter image description here

使用此图像,Tesseract完全能够识别文本

from PIL import Image
import pytesseract

image_file = Image.open('purple.png')

image_file = image_file.convert('L') # convert image to black and white
image_file.save('question.png')
text = pytesseract.image_to_string(image_file, lang = 'eng',config='-psm 6')
print(text)

Tesseract输出:

  

这些元素中的哪一个是其中的气体   标准状态?