0

I have working code in which I use tesseract to extract data from image file, as follows:

if src:
    driver.get(src)
    driver.save_screenshot('/Users/username/script/' + 'test.png')
    image_name = 'test.png'
    im = Image.open(image_name)
    image_text = pytesseract.image_to_string(im)
    print '\nImage Text:\t', image_text

This code snippet works without any error in Mac terminal when I execute code but when I do the same in Eclipse using PyDev, it throws an error:

Exception:  [Errno 2] No such file or directory

when trying to execute the line:

im = Image.open(image_name)

Why is this happening in Eclipse?

UPDATE: Since my code seemed funky to few people, I have changed it as following but the issue still remains (runs perfectly fine on mac terminal but Eclipse keeps giving me same error)

if src:
    driver.get(src)
    image_name = 'test.png'
    image_path = os.path.realpath(image_name)
    driver.save_screenshot(image_path)

    # read chart data from image
    im = Image.open(image_path)
smci
  • 32,567
  • 20
  • 113
  • 146
user1596115
  • 321
  • 1
  • 5
  • 17
  • 1
    I guess the image is not in the working directory. You can adapt the working directory in the run configuration or use an absolute file name (with path). – Klaus D. Nov 28 '16 at 08:40
  • Can you try with complete path to read image. Image.open("/Users/username/script/test.png") – SunilThorat Nov 28 '16 at 08:41
  • @KlausD. image is indeed in the same working directory and that is why it would work in terminal? – user1596115 Nov 28 '16 at 09:10
  • @SunilT I get the same exception when using the full path `image_name = '/Users/username/script/test.png'` – user1596115 Nov 28 '16 at 09:10
  • Follow this post once-http://stackoverflow.com/questions/9765227/python-ioerror-errno-2-no-such-file-or-directory – Shoaib Akhtar Nov 28 '16 at 09:23
  • @ShoaibAkhtar I tried your suggestion but ended up with same error `curr_dir = os.getcwd() temp_name = 'test.png' image_name = os.path.join(curr_dir, temp_name) im = Image.open(image_name) ` – user1596115 Nov 28 '16 at 09:41

2 Answers2

1

So finally figured out the issue by using traceback as suggested by @Fabio. It was NOT related to image file not present in the current directory but problem with not finding tesseract in path.

Inside pytesseract.py file, it reads:

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'

Note that changing the pytesseract.py file directly is not a good idea but in whatever file you are importing pytesseract, add the following line (path to tesseract will depend on your particular machine...in mac I was able to find path by using which command: which tesseract)

pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'
user1596115
  • 321
  • 1
  • 5
  • 17
0

Your code is a bit strange... you save with a path and then just use the image_name afterwards.

Try it with the following code:

import os

filename = '/path/to/the/image.png'
driver.save_screenshot(filename)
if not os.path.exists(filename):
    raise AssertionError("Image: %s does not exist" % (filename,))
Image.open(filename)

if it doesn't work, paste the full contents of the stacktrace.

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • I tried with your code and the stacktrace is still the same `Exception: [Errno 2] No such file or directory` – user1596115 Nov 28 '16 at 12:39
  • I have updated my code based on your comment (see above) but still the same exception – user1596115 Nov 28 '16 at 12:53
  • In your new code, you didn't add the os.path.exists() call I outlined... Also, please post the **full** contents of the exception, not just the final bit. – Fabio Zadrozny Nov 28 '16 at 17:22
  • That is the full content of the exception, please see the screenshot in the link [here](https://expirebox.com/download/413e2ce0ad81f6d1db90d00e9f003644.html). I have also included you os.path.exists() call. – user1596115 Nov 28 '16 at 18:03
  • Humm, in your except clause, you shouldn't print the exception as you're doing, rather, print it as: `import traceback;traceback.print_exc()` in your exception handler to get the **full** stack trace -- PyDev actually has a template for that, so, you can write `printexc` Ctrl+Space and get those contents written for you ;) – Fabio Zadrozny Nov 28 '16 at 18:24
  • thanks for the useful info. I guess I would use traceback more often in my code. Traceback was long so I could not post it here but it is same as [this](http://stackoverflow.com/questions/28741563/pytesseract-no-such-file-or-directory-error) so looks like I need to change value for setting config in `/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py` – user1596115 Nov 28 '16 at 19:08
  • Thanks again, traceback was very useful. Problem was fixed by explicitly defining the path of tesseract as mentioned in pytesseract package `pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'` – user1596115 Nov 28 '16 at 19:30