如何从图像中读取纯英文字母或数字的文本

2024-10-02 20:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从图像中获取文本,但在我的图像中它只有一个数字,可以是1-9之间的任何值,我需要得到那个数字。我用pytesseract来做这个,但是在阅读时它显示的是空文本。 以下是我的形象:

Single alphabet/number image

以下是我正在尝试的:

from PIL import Image, ImageEnhance, ImageFilter
import pytesseract


def getText(image):
    image = Image.open(image)
    image.show()
    image = image.point(lambda x: 0 if x < 143 else 255)  # To clean Image
#     text = pytesseract.image_to_string(image).encode('utf-8').strip()
    text = pytesseract.image_to_string(image)
    return text


image1 = '/home/einfochips/Documents/Kroger_Automation_Framework/src/main/scripts/background.png'
txt1 = getText(image1)
print txt1, '_______________', type(txt1), len(txt1)

Tags: totextfrom图像image文本importstring