Python 3.4中的Pytesser:名稱“image_to_string”沒有定義?

[英]Pytesser in Python 3.4: name 'image_to_string' is not defined?


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.

首先,我想說我知道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')

No problems there, but when I use

這里沒有問題,但是當我使用的時候

print (image_to_string(image))

it comes up with this:

結果是這樣的:

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

2 个解决方案

#1


3  

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不起作用。原因是,當您從pytesser導入*(或簡單地導入它)時,如果__name__ == '__main__'條件將是正確的,下面的代碼將運行。

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.

我相信您已經知道,在Python 3中,print不再是一個語句,而是一個函數。因此,行打印文本將出現一個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.

我不知道為什么您沒有在代碼中看到這個SyntaxError,但是如果這個錯誤是悄無聲息地傳遞的,那就意味着首先沒有導入任何東西,因此出現了錯誤。

To fix this, use Python 2.7.

要解決這個問題,請使用Python 2.7。

Python 2.7:

Python 2.7:

>>> from pytesser import *
>>> print image_to_string
<function image_to_string at 0x10057ec08>

Python 3:

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

#2


0  

I had a similar problem using the module pytesseract Python 3. You may need to change the import statement in init.py for the pytesser module and add a leading dot. For pytesseract running 2to3-3.4 on init.py it changed from:

我在使用pytesseract Python 3模塊時遇到了類似的問題。您可能需要在init中更改import語句。為pytesser模塊添加一個前導點。對於在init上運行2to3-3.4的pytesseract。py它改變了:

from pytesseract import image_to_string

從pytesseract進口image_to_string

to

from .pytesseract import image_to_string

從.pytesseract進口image_to_string

and then it can resolve the image_to_string function.

然后它可以解析image_to_string函數。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/01/01/4f4ec7c781e6b20dabd42b1cf3611c3.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com