为什么pytesseract会抛出WinError 6?

时间:2017-01-11 21:52:21

标签: python python-3.x subprocess python-tesseract

这是我第一次使用pytesseract。我试图在小图像上执行简单的OCR。代码归结为:

from PIL import Image
from pytesseract import image_to_string

test = Image.open(r'C:\test.jpg')
print(image_to_string(test))

抛出OSError:[WinError 6]句柄无效

Traceback (most recent call last):
  File "C:\Testing.py", line 5, in <module>
    print(image_to_string(test))
  File "C:\\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\\subprocess.py", line 911, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\\subprocess.py", line 1150, in _get_handles
    c2pwrite = self._make_inheritable(c2pwrite)
  File "C:\\subprocess.py", line 1182, in _make_inheritable
    _winapi.DUPLICATE_SAME_ACCESS)
OSError: [WinError 6] The handle is invalid

我在Windows 7上使用Python 3.5。

提前感谢您的时间!

2 个答案:

答案 0 :(得分:1)

我不知道真正的问题但是一旦我重新启动电脑就解决了问题。

答案 1 :(得分:0)

我有同样的问题..所以我不太了解Python来解决所有各种文件,但我确实注意到问题是由于子进程文件。不知何故,当pytesseract调用subprocess时,脚本会抛出错误。

无论如何,我通过完全跳过pytesseract并通过子进程访问tesseract来解决这个问题。我的代码如下:

import subprocess
from subprocess import Popen, PIPE
image_file = r"C:\Temp\test.png"
outlocation = r"C:\Temp\output"
command = ['tesseract', image_file, outlocation]
proc = subprocess.Popen(command,
        stderr=subprocess.PIPE)

这是一个乐队援助解决方案,但它允许我继续在python中使用tesseract。希望它有所帮助。

此外,上面的解决方法假设您已经安装了tesseract。如果你没有,你可以从https://github.com/UB-Mannheim/tesseract/wiki

获取它