Pytesseract No such file or directory error

2019-02-22 08:03发布

问题:

First of all I did everything mentioned here pytesseract-no such file or directory error

Still doesn't work. Now I'm using Pycharm IDE with following code:

from PIL import Image
import pytesseract
import subprocess

im = Image.open('test.png')
im.show()
subprocess.call(['tesseract','test.png','out'])
print pytesseract.image_to_string(Image.open('test.png'))
  • im.show() opens the image successfully.
  • subprocess.call() with tesseract test.png out also extracts the text from the image..
  • but pytesseract.image_to_string() fails.

I don't get it. Why I am able to use tesseract in shell but not in python. And in python I can open same image but when used with tesseract Image can't be found.

Below you can see the error output.

 File "/home/hamza-c/Schreibtisch/Android/JioShare/orc.py", line 7, in <module>
    print pytesseract.image_to_string(Image.open('/home/hamza-c/Schreibtisch/Android/JioShare/test.png'))
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 162, in image_to_string
    config=config)
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 95, in run_tesseract
    stderr=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1340, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

回答1:

I tested the code you mentioned in your question. It works fine. I was facing the same error

No such file or directory found

The problem was the directory containing 'tesseract.exe' was not added to the environment Variable. You should be able to run command 'tesseract' in command prompt.

if tesseract is not installed you can download it from tesseract 1: https://github.com/tesseract-ocr/tesseract/wiki and for windows use third party installer available here



回答2:

maybe you need install tesseract ,if your os is centos, please enter

yum install tesseract


回答3:

I've used the following command and it worked for me:

brew install tesseract


回答4:

I solved my own question.

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

It's still unclear why it works when a reference is passed but not directly when I try to open image inside the parameter.