Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python error when importing image_to_string from tesseract

I recently used tesseract OCR with python and I kept getting an error when I was trying to import image_to_string from tesseract.

Code causing the problem:

# Perform OCR using tesseract-ocr library
from tesseract import image_to_string
image = Image.open('input-NEAREST.tif')
print image_to_string(image)

Error caused by above code:

Traceback (most recent call last):  
file "./captcha.py", line 52, in <module>  
from tesseract import image_to_string  
ImportError: cannot import name image_to_string

I've verified that the tesseract module is installed:

digital_alchemy@roaming-gnome /home $ pydoc modules | grep 'tesseract'
Hdf5StubImagePlugin _tesseract          gzip                sipconfig
ORBit               cairo               mako                tesseract

I believe that I've grabbed all the required packages but unfortunately I'm just stuck at this point. It appears that the function is not in the module.

Any help greatly appreciated.

like image 358
digital_alchemy Avatar asked Feb 01 '13 05:02

digital_alchemy


People also ask

How do I import Pytesseract into Jupyter notebook?

Create a Python script (a . py-file), or start up a Jupyter notebook. At the top of the file, import pytesseract , then point pytesseract at the tesseract installation you discovered in the previous step. Note the r' ' at the start of the string that defines the file location.

How do you find the Pytesseract path?

Default installation path at the time of this edit was: C:\Users\USER\AppData\Local\Tesseract-OCR. It may change so please check the installation path.


2 Answers

Another possibility that seems to have worked for me is to modify pytesseract so that instead of import Image it has from PIL import Image

Code that works in PyCharm after modifying pytesseract:

from pytesseract import image_to_string
from PIL import Image

im = Image.open(r'C:\Users\<user>\Downloads\dashboard-test.jpeg')
print(im)

print(image_to_string(im))

Pytesseract I installed via the package management built into PyCharm

like image 130
Logan Avatar answered Sep 17 '22 10:09

Logan


Is your syntax correct for the module you have installed? That image_to_string functions looks like it is from PyTesser per the usage example on this page: https://code.google.com/p/pytesser/

Your import looks like it is for python-tesseract which has a more complicated usage example listed: https://code.google.com/p/python-tesseract/

like image 26
m.brindley Avatar answered Sep 18 '22 10:09

m.brindley