curious_cosmo
curious_cosmo

Reputation: 1214

Using optical character recognition in python script

I'd like to accomplish the seemingly simple task of running a python script that uses OCR to give me a string of text from an image. My code:

from PIL import Image
from pytesseract import *

image_file = 'IMG_9296'
im = Image.open(image_file)
text = image_to_string(im)
text = image_file_to_string(image_file)
text = image_file_to_string(image_file, graceful_errors=True)
print "=====output=======\n"
print text

However, I'm running into trouble in the second line. I successfully installed pillow, PIL, and pytesseract on my Mac OS X (running Python 2.7, Pillow-5.0.0 pytesseract-0.2.0). But I'm getting this error:

Traceback (most recent call last):
File "./HQcode2.py", line 2, in <module>
from pytesseract import *
File "/Library/Python/2.7/site-packages/pytesseract/__init__.py", line 
1, in <module>
from .pytesseract import (
File "/Library/Python/2.7/site-packages/pytesseract/pytesseract.py", 
line 9, in <module>
import Image
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 27, in 
<module>
from . import VERSION, PILLOW_VERSION, _plugins
ValueError: Attempted relative import in non-package

I've looked up this error but haven't found anything helpful yet for my case. What seems to be the problem here, and how can I fix it?

Upvotes: 1

Views: 976

Answers (1)

the4thv
the4thv

Reputation: 591

Can you confirm you have tesseract installed too? (https://github.com/madmaze/pytesseract#installation)

Based on this issue: https://github.com/madmaze/pytesseract/issues/58, the author of the package seems to indicate that your issue is a missing dependency.

Upvotes: 1

Related Questions