3

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)

Tobias Woods
  • 35
  • 1
  • 4
  • Are you running it from the same directory where test.png is? – Devstr Feb 20 '18 at 00:31
  • Could you please add the exact command that you use for running the script? – Devstr Feb 20 '18 at 00:34
  • To be honest, i'm not quite sure. I'm running this short program in terminal, and I have a picture named 'test.png' on my desktop, so would that be in the same directory? – Tobias Woods Feb 20 '18 at 00:36
  • To run this program, I open terminal, then type "Python", then I type in my code – Tobias Woods Feb 20 '18 at 00:39
  • Copy your Python script (say it's called myscript.py) to your Desktop. Then do `cd ~/Desktop` in the Terminal. Then run the script like this : `python myscript.py` – Devstr Feb 20 '18 at 00:41
  • I recommend going through basic bash tutorial to get more comfortable with the command line. For example http://blog.teamtreehouse.com/introduction-to-the-mac-os-x-command-line – Devstr Feb 20 '18 at 00:44
  • Alternatively, you can open new Terminal, then `cd Desktop`, then type `python `, then type your commands – Devstr Feb 20 '18 at 00:48
  • Yes! It worked! Thanks – Tobias Woods Feb 20 '18 at 00:59

1 Answers1

3

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.

Devstr
  • 4,431
  • 1
  • 18
  • 30
  • Ok, I successfully downloaded tesseract and then tesseract-orc, but I still got the error " IOError: [Errno 2] No such file or directory: 'test.png' ". What should I do from here? – Tobias Woods Feb 19 '18 at 23:45
  • Well, does `test.png` exist? It's hard to tell without seeing your code. Please add it to the question. Also please have a look at how to create [mcve] – Devstr Feb 19 '18 at 23:54