Python 3.4中的Pytesser:名称'image_to_string'未定义?
首先,我想说我知道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
-
您的代码不适用于Python3。原因是因为这样做
from pytesser import *
(或只是首先将其导入)时,if __name__ == '__main__'
条件将为True,并且其下面的代码将运行。如您所知,在Python 3中,
print
它不再是语句,而是函数。因此,aSyntaxError
将出现在该行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