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

发布于 2021-01-29 15:04:40

首先,我想说我知道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
关注者
0
被浏览
52
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您的代码不适用于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
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看