首先,我想说我知道 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
最佳答案
您的代码不适用于 Python 3。原因是当您执行 from pytesser import *
(或简单地首先导入它)时,if __name__ == ' __main__'
条件将为 True,并且其下面的代码将运行。
我相信您知道,在 Python 3 中,print
不再是语句,而是函数。因此,在 print text
行将出现 SyntaxError
。
我不确定为什么您在代码中没有看到此 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
关于python - Python 3.4 中的 Pytesser : name 'image_to_string' is not defined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20874106/