14

This is my problem, I want to use pytesser to get a picture's contents. My operating system is Mac OS 10.11, and I have already installed PIL, pytesser, tesseract-ocr engine, and other supporting libraries like libpng and so on. But when I run my code, as below, error occurs.

from pytesser import *
import os
image = Image.open('/Users/Grant/Desktop/1.png')
text = image_to_string(image)
print text

Next is the error message

Traceback (most recent call last):
File "/Users/Grant/Documents/workspace/image_test/image_test.py",    line 10, in <module>
text = image_to_string(im)
File   "/Users/Grant/Documents/workspace/image_test/pytesser/pytesser.py", line   30, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "/Users/Grant/Documents/workspace/image_test/pytesser/pytesser.py", line 21, in call_tesseract
retcode = subprocess.call(args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Besides, tesseract-ocr engine runs well on my Mac, I can run it in terminal and get the result. Below is the test picture result. tesseract result

Can anyone help me with this question,please?

grant
  • 141
  • 1
  • 1
  • 4
  • Have you tried using \\ instead of / ? – user987654321 Feb 24 '16 at 18:27
  • I just tried, it does not work. It returns an error message" IOError: [Errno 2] No such file or directory: '\\Users\\Grant\\Desktop\\1.png' " – grant Feb 24 '16 at 19:55
  • You need to install Tesseract OCR engine and provide its path. [Same problem discussed here.](https://stackoverflow.com/questions/41652335/pytesseract-error-windows-error-error-2/46363594#46363594) – Abdul Sep 22 '17 at 11:28

7 Answers7

14

Luckily, I solved this one.

At first, I run the command

pip install pytesseract

to install the package.

But I get the error message of 'No such file or directory using pytesser'.

Then I read this link: image_to_string doesn't work in Mac So, just run the following script:

brew link libtiff 
brew link libpng 
brew link jpeg
brew install tesseract

Worked for me ~

Community
  • 1
  • 1
CoderYel
  • 228
  • 2
  • 8
10

I had the same problem, but i managed to convert image to string. using apt Advanced package tool should do the trick:

sudo apt install tesseract-ocr

and if you can't use it in a python script just do this:

from os import system

system("tesseract -l eng /image.png text.txt")
Liam
  • 6,009
  • 4
  • 39
  • 53
8

Open file pytesseract.py.

Mine is in /Users/yourUser/.virtualenvs/cv/lib/python2.7/site-packages/pytesseract/pytesseract.py

Change tesseract_cmd = 'tesseract' to tesseract_cmd = '/usr/local/bin/tesseract'

hoangpx
  • 470
  • 2
  • 16
  • 1
    I tried linking and changing paths over and over, none of this worked. This was the only solution that worked for me. Thanks! – user1610719 Apr 15 '16 at 13:44
  • `# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY`, just a line ahead the involded line :-) so, adding tesseract to the `$PATH` would make it work too without touch the file. Once updated, you'll need to edit again, so the best if you add it to path at first time. Case of mine the binary is called `pytesseract` so none will work unless you add the proper name at all. – m3nda Aug 08 '16 at 02:23
  • @em3nda What do you mean with your last sentence? Are you saying changing Path doesn't work for you? Despite my path having the bin folder to tesseract, it still doesn't work. – agent18 Apr 16 '18 at 21:06
4

You're getting exception because subprocess isn't able to find the binaries (tesser executable).

The installation is a 3 step process:

1.Download/Install system level libs/binaries:

For various OS here's the help. For MacOS you can directly install it using brew.

Install Google Tesseract OCR (additional info how to install the engine on Linux, Mac OSX and Windows). You must be able to invoke the tesseract command as tesseract. If this isn’t the case, for example because tesseract isn’t in your PATH, you will have to change the “tesseract_cmd” variable at the top of tesseract.py. Under Debian/Ubuntu you can use the package tesseract-ocr. For Mac OS users. please install homebrew package tesseract.


2.Install Python package

pip install pytesseract

3.Finally, you need to have tesseract binary in you PATH.

Or, you can set it at run-time:

import pytesseract

pytesseract.pytesseract.tesseract_cmd = '<path-to-tesseract-bin>'

The default path 'd be /usr/local/bin/tesseract

Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
2

I run into the same problem twice, for both MacOS and Ubuntu. This worked with me. Hoping it can help.

First, open Terminal, then:

Chau Pham
  • 4,705
  • 1
  • 35
  • 30
2

You need to install tesseract-ocr:

sudo apt-get install tesseract-ocr

And in the script

    from PIL import Image
    import os
    import pytesseract

    text = pytesseract.image_to_string(Image.open(os.path.abspath('test.png')))
0

This might not be the case for everybody but I was having a similar issue and it was due to getting errors when installing tesseract. I kept getting the error message:

Making install in ccutil
/bin/sh: /Applications/Xcode: No such file or directory
make: *** [install-recursive] Error 1

This was due to me having previously renamed /Applications/Xcode to /Applications/Xcode 8 in order to make it easier for myself to distinguish between different Xcode versions installed on my system.

I temporarily renamed it back to /Applications/Xcode then ran the command

sudo xcode-select --switch /Applications/Xcode.app

Then finally tried to reinstall tesseract and thankfully got no error messages this time.

brew install tesseract --all-languages

Now Python code runs fine and I get no "OSError: [Errno 2] No such file or directory" error message.

Eddie
  • 86
  • 5