Python - 使用Pytesseract读取图像中的数字

时间:2017-03-19 01:23:32

标签: python

我正在使用pyautogui和pytesseract的组合捕获屏幕上的小区域,然后将数字/文本拉出该区域。我编写的脚本可以完美地读取大部分捕获的图像,但是单位数字似乎会导致问题。例如,包含数字的图像的小区域保存到.png文件中,数字11,14和18被完美地拉出,但数字7只是作为空白字符串返回。

问题:可能导致这种情况发生的原因是什么?

代码:大幅缩小以使其易于理解:

def get_text(image):
    return pytesseract.image_to_string(image)

answer2 = pyautogui.screenshot('answer2.png',region=(727, 566, 62, 48))
img = Image.open('answer2.png')
answer2 = get_text(img)

此代码重复4次,每张图片一次,适用于11,14,18但不适用于7。

只是为了减慢这里读取的文件的速度,是通过截图命令保存后的图像截图。

https://gyazo.com/0acbf5be2d970abeb29561113c171fbe

这是我工作的截图:

https://gyazo.com/311913217a1302382b700b07ad3e3439

1 个答案:

答案 0 :(得分:2)

我找到问题Why pytesseract does not recognise single digits?并在评论中找到了选项-psm 6

我使用选项tesseract检查了-psm 6,它可以识别您图片上的单个数字。

tesseract -psm 6 number-7.jpg result.txt

我使用选项image_to_string检查了config='-psm 6',它也可以识别您图片上的单个数字。

#!/usr/bin/env python3

from PIL import Image
import pytesseract

img = Image.open('number-7.jpg')

print(pytesseract.image_to_string(img, config='-psm 6'))