Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用Pytesseract读取图像中的数字_Python - Fatal编程技术网

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

Python-使用Pytesseract读取图像中的数字,python,Python,我使用pyautogui和pytesseract的组合来捕获屏幕上的小区域,然后从区域中提取数字/文本。我写的脚本已经完美地阅读了大部分捕获的图像,但是一位数的数字似乎引起了问题。例如,包含数字的图像的小区域被保存到.png文件中。数字11、14和18被完美地提取出来,但数字7只是作为空白字符串返回 问题:是什么导致了这种情况 代码:大幅缩小,使每一项操作都易于遵循: def get_text(image): return pytesseract.image_to_string(imag

我使用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无效

为了减缓文件的读取速度,这里有一个通过screenshot命令保存后的图像截图

以下是我工作的屏幕截图:

我找到了问题,在评论中我找到了选项
--psm 6

我用选项
--psm 6
选中了
teseract
,它可以识别图像上的单个数字

tesseract --psm 6 number-7.jpg result.txt
我使用选项
config='--psm 6'
选中了
pytesseract.image\u to_string()
,它也可以识别图像上的单个数字

#!/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'))
请参阅中的注释
tesseract
带有选项
psm 6
可以识别您的单个数字,但我不知道如何将此选项与
pytesseract.image\u to\u string一起使用