我正在使用下面的代码。但是,这个简单的图像中,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处理?
更新
我现在使用了这段代码:
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)
输出:
请帮助我为什么它不起作用。
答案 0 :(得分:3)
使用image.convert('-1')
即可获得此图片
这是一张带有很多噪音的照片,所以Tesseract会失败。
我建议您使用image_file.convert('L')
将其转换为更好的灰度图像(有关'1'
模式和'L'
模式的详细信息,请查看documentation)。使用'L'
模式,您将获得
使用此图像,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输出:
这些元素中的哪一个是其中的气体 标准状态?