首先,我想说的是,我知道pytesser不适用于Python3.4,但我从http://ubuntuforums.org/archive/index.php/t-1916011.html上看到pytesser也适用于python3。我刚刚安装了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发布于 2014-01-02 07:41:54
您的代码不能在Python3上运行,原因是当您执行from pytesser import * (或者首先简单地导入它)时,if __name__ == '__main__'条件将为True,并且它下面的代码将会运行。
我相信您已经知道,在Python3中,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发布于 2015-03-23 06:46:24
我在使用pytesseract Python3模块时遇到了类似的问题。您可能需要在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函数了。
发布于 2019-08-11 17:54:22
我是这样解决这个问题的:
from pytesseract import pytesseract as pytesser
from PIL import Imagehttps://stackoverflow.com/questions/20874106
复制相似问题