Python 3.4中的Pytesser:名称“image_to_string”没有定义?

[英]Pytesser in Python 3.4: name 'image_to_string' is not defined?

First off, I would like to say that I know pytesser is not for Python 3.4, but I read from that pytesser should also work for Python 3. I just installed pytesser and I am trying to read a file.

首先,我想说我知道pytesser不适合Python 3.4,但是我从中读到pytesser也应该适合Python 3。我刚刚安装了pytesser,我正在尝试读取一个文件。

from pytesser import *
from PIL import Image
image ='/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

2 个解决方案



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.

您的代码对Python 3不起作用。原因是,当您从pytesser导入*(或简单地导入它)时,如果__name__ == '__main__'条件将是正确的,下面的代码将运行。

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.

我相信您已经知道,在Python 3中,print不再是一个语句,而是一个函数。因此,行打印文本将出现一个SyntaxError。

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。

Python 2.7:

Python 2.7:

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

Python 3:

Python 3:

>>> from pytesser import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./", line 61
    print text
SyntaxError: invalid syntax



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

我在使用pytesseract Python 3模块时遇到了类似的问题。您可能需要在init中更改import语句。为pytesser模块添加一个前导点。对于在init上运行2to3-3.4的pytesseract。py它改变了:

from pytesseract import image_to_string



from .pytesseract import image_to_string


and then it can resolve the image_to_string function.




粤ICP备14056181号  © 2014-2021