python - Python 3.4 中的 Pytesser : name 'image_to_string' is not defined?

标签 python ocr

首先,我想说我知道 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/

相关文章:

iphone - 使用 Tesseract 从图像中异步读取文本?

web-applications - 如何扫描到 Web 应用程序

ios - Tesseract OCR 相机

python mahotas : Applying threshold filter and saving image as pgn

python - 在Python中读取csv文件时列出索引超出范围错误

python - 从python速成类(class)Web应用程序Django项目

python - 有什么方法可以使用 itertools groupby 删除列表中相邻的重复项,但保留原始索引?

Python 请求未返回与浏览器请求/cURL 相同的 header

python - Pandas - 展平一列字典列表

python - OpenCV:为 OCR 隔离车牌字符