2

First off, I would like to say that I know pytesser is not for Python 3.4, but I read from http://ubuntuforums.org/archive/index.php/t-1916011.html that pytesser should also work for Python 3. I just installed pytesser and I am trying to read a file.

from pytesser import *
from PIL import Image
image = Image.open('/Users/William/Documents/Science/PYTHON/textArea01.png')

No problems there, but when I use

print (image_to_string(image))

it comes up with this:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    print (image_to_string(image))
NameError: name 'image_to_string' is not defined
user3151828
  • 363
  • 1
  • 7
  • 21
  • Please show us the whole code. Your error says `File "", line 1, in `, hence, you did no importing – TerryA Jan 01 '14 at 22:56
  • I just looked at the source.... it does not appear to me that it would be Python 3 compatible... the only reason I say that is that it imports StringIO at the top. – clutton Jan 01 '14 at 23:00
  • @Haidro My whole code is already what I posted. The import was done in the 'from pytesser import *' part. – user3151828 Jan 01 '14 at 23:04
  • Yes, but in your error, it says that `print (image_to_string(image))` is the first line – TerryA Jan 01 '14 at 23:05
  • @Haidro Well, I was typing all this in the shell, I used print(image_to_string(image)) after all the other stuff. Should I have done this in a script? – user3151828 Jan 01 '14 at 23:25
  • Try that. When I use the interactive interpreter, it works fine for me. But it could be the fact that this doesn't work for python 3 (I was using 2.7) – TerryA Jan 01 '14 at 23:34

3 Answers3

4

Your code will not work for Python 3. The reason is because when you do from pytesser import * (or simply import it in the first place), the if __name__ == '__main__' conditional will be True, and the code below it will run.

As I'm sure you're aware, in Python 3, print is no longer a statement but a function. Hence, a SyntaxError will occur at the line print text.

I'm not sure why you're not seeing this SyntaxError in your code, but if this error passed silently, that means that nothing was imported in the first place, hence the error.

To fix this, use Python 2.7.

Python 2.7:

>>> from pytesser import *
>>> print image_to_string
<function image_to_string at 0x10057ec08>

Python 3:

>>> from pytesser import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./pytesser.py", line 61
    print text
             ^
SyntaxError: invalid syntax
TerryA
  • 58,805
  • 11
  • 114
  • 143
0

I had a similar problem using the module pytesseract Python 3. You may need to change the import statement in init.py for the pytesser module and add a leading dot. For pytesseract running 2to3-3.4 on init.py it changed from:

from pytesseract import image_to_string

to

from .pytesseract import image_to_string

and then it can resolve the image_to_string function.

oenpelli
  • 3,467
  • 2
  • 29
  • 20
-1

I solved this problem like this:

from pytesseract import pytesseract as pytesser
from PIL import Image
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68