pytesseract: Can't access Image with “Image.open” “Errno 2” error

我的未来我决定 提交于 2021-01-27 20:32:32

问题


I'm trying to use pytesseract for the first time. I'm also not so confortable with python. I've created a new folder called python_test on my desktop. I'm on Mac. In this folder I have a test.png file and a py script :

from pytesseract import image_to_string
from PIL import Image

print image_to_string(Image.open('test.png'))
print image_to_string(Image.open('test-english.jpg'), lang='eng')

So from my terminal, I'm going into the python_test folder then I'm running python read.py then I have the following error :

Traceback (most recent call last):
  File "read.py", line 4, in <module>
    print image_to_string(Image.open('test.png'))
  File "/anaconda/anaconda/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 161, in image_to_string
    config=config)
  File "/anaconda/anaconda/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "/anaconda/anaconda/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/anaconda/anaconda/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

What I'am doing wrong ?


回答1:


I got the same error as you, installing the tesseract package fixed it (or tesseract-ocr on debian/ubuntu). It contains the native code library used under the hood by pytesseract.

An image load error seems like an odd way for the library to fail if the underlying native library is not installed, but there you go.

To install use commands (insert sudo as appropriate)

macos

brew install tesseract

ubuntu

apt install tesseract-ocr



回答2:


I also had the error when I used first time image_to_string.

You have to change the the following line in the pytesseract.py file.

tesseract_cmd = 'C:\\Tesseract-OCR\\tesseract'

Note: I'm using windows.



来源:https://stackoverflow.com/questions/43989929/pytesseract-cant-access-image-with-image-open-errno-2-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!