Search code examples
pythonmacostesseractpython-tesseract

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


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 ?


Solution

  • 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