Python 3.4中的Pytesser:名称'image_to_string'未定义?

时间:2014-01-01 22:47:14

标签: python ocr

首先,我想说我知道pytesser不适用于Python 3.4,但我从http://ubuntuforums.org/archive/index.php/t-1916011.html读到pytesser也适用于Python 3。 我刚刚安装了pytesser,我正在尝试读取文件。

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

没有问题,但是当我使用

print (image_to_string(image))

它提出了这个:

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

3 个答案:

答案 0 :(得分:3)

您的代码不适用于Python 3.原因是当您执行from pytesser import *(或者只是首先导入它)时,if __name__ == '__main__'条件将为True,并且代码如下它会运行。

我确信你知道,在Python 3中,print不再是一个语句而是一个函数。因此,SyntaxError将出现在print text行。

我不确定你为什么在你的代码中没有看到这个SyntaxError,但如果这个错误无声地传递,那就意味着首先没有导入任何内容,因此错误。

要解决此问题,请使用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

答案 1 :(得分:0)

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

from pytesseract import image_to_string

from .pytesseract import image_to_string

然后它可以解析image_to_string函数。

答案 2 :(得分:0)

我这样解决了这个问题:

relationship