Search code examples
python-2.7tesseract

Can't install Tesseract-OCR on Mac


I'm trying to make an OCR program in python 2.7.14 with pytesseract. When I ran my code:

from PIL import Image
import pytesseract

print(pytesseract.image_to_string(Image.open('test.png')))

I got the error:

IOError: [Errno 2] No such file or directory: 'test.png'

I searched in many places, and it seems that I need to install tesseract-ocr. I ran:

pip install tesseract-ocr

But I got the error:

Collecting tesseract-ocr Using cached tesseract-ocr-0.0.1.tar.gz Requirement already satisfied: cython in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from tesseract-ocr) Installing collected packages: tesseract-ocr Running setup.py install for tesseract-ocr ... error Complete output from command /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;file='/private/var/folders/rd/lf95py7d7s3dkzft38jh3m8h0000gn/T/pip-build-DTR_fL/tesseract-ocr/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /var/folders/rd/lf95py7d7s3dkzft38jh3m8h0000gn/T/pip-U3OoHi-record/install-record.txt --single-version-externally-managed --compile: running install running build running build_py file tesseract_ocr.py (for module tesseract_ocr) not found file tesseract_ocr.py (for module tesseract_ocr) not found running build_ext building 'tesseract_ocr' extension creating build creating build/temp.macosx-10.6-intel-2.7 /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c tesseract_ocr.cpp -o build/temp.macosx-10.6-intel-2.7/tesseract_ocr.o tesseract_ocr.cpp:558:10: fatal error: 'leptonica/allheaders.h' file not found #include "leptonica/allheaders.h" ^ 1 error generated. error: command '/usr/bin/clang' failed with exit status 1

Command "/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;file='/private/var/folders/rd/lf95py7d7s3dkzft38jh3m8h0000gn/T/pip-build-DTR_fL/tesseract-ocr/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /var/folders/rd/lf95py7d7s3dkzft38jh3m8h0000gn/T/pip-U3OoHi-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/rd/lf95py7d7s3dkzft38jh3m8h0000gn/T/pip-build-DTR_fL/tesseract-ocr/

Is there anyway I can fix this? (This same error occurred when I tried other ways of installing it, like sudo)


Solution

  • You need to install the tesseract itself.

    As it says in documentation https://github.com/tesseract-ocr/tesseract/wiki#homebrew

        brew install tesseract 
    

    Also I'd recommend installing python through brew as well. That way you won't pollute system python installation.

    And it's best to use virtualenv too.