Python 3.4中的Pytesser:名称'image_to_string'未定义? [英] Pytesser in Python 3.4: name 'image_to_string' is not defined?

查看:119
本文介绍了Python 3.4中的Pytesser:名称'image_to_string'未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说的是,我知道pytesser不适用于Python 3.4,但是我从 http://ubuntuforums.org/archive/index.php/t-1916011.html pytesser也应适用于Python 3. 我刚刚安装了pytesser,正在尝试读取文件.

First off, I would like to say that I know pytesser is not for Python 3.4, but I read from http://ubuntuforums.org/archive/index.php/t-1916011.html that pytesser should also work for Python 3. I just installed pytesser and I am trying to read a file.

from pytesser import *
from PIL import Image
image = Image.open('/Users/William/Documents/Science/PYTHON/textArea01.png')

没有问题,但是当我使用

No problems there, but when I use

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 *(或首先将其导入)时, conditional将为True,并且其下面的代码将运行.

Your code will not work for Python 3. The reason is because when you do from pytesser import * (or simply import it in the first place), the if __name__ == '__main__' conditional will be True, and the code below it will run.

我确定您知道,在Python 3中,print不再是语句,而是函数.因此,SyntaxError将出现在print text行.

As I'm sure you're aware, in Python 3, print is no longer a statement but a function. Hence, a SyntaxError will occur at the line print text.

我不确定为什么您在代码中没有看到此SyntaxError,但是如果此错误以静默方式通过,则意味着首先没有导入任何内容,因此是该错误.

I'm not sure why you're not seeing this SyntaxError in your code, but if this error passed silently, that means that nothing was imported in the first place, hence the error.

要解决此问题,请使用Python 2.7.

To fix this, use Python 2.7.

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 3.4中的Pytesser:名称'image_to_string'未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆