首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3.4中的Pytesser :没有定义名称'image_to_string‘?

Python3.4中的Pytesser :没有定义名称'image_to_string‘?
EN

Stack Overflow用户
提问于 2014-01-02 06:47:14
回答 3查看 11.8K关注 0票数 2

首先,我想说的是,我知道pytesser不适用于Python3.4,但我从http://ubuntuforums.org/archive/index.php/t-1916011.html上看到pytesser也适用于python3。我刚刚安装了pytesser,我正在尝试读取一个文件。

代码语言:javascript
复制
from pytesser import *
from PIL import Image
image = Image.open('/Users/William/Documents/Science/PYTHON/textArea01.png')

没有问题,但是当我使用

代码语言:javascript
复制
print (image_to_string(image))

它想出了这个:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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:

代码语言:javascript
复制
>>> from pytesser import *
>>> print image_to_string
<function image_to_string at 0x10057ec08>

Python 3:

代码语言:javascript
复制
>>> from pytesser import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./pytesser.py", line 61
    print text
             ^
SyntaxError: invalid syntax
票数 4
EN

Stack Overflow用户

发布于 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函数了。

票数 0
EN

Stack Overflow用户

发布于 2019-08-11 17:54:22

我是这样解决这个问题的:

代码语言:javascript
复制
from pytesseract import pytesseract as pytesser
from PIL import Image
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20874106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档