从 tesseract 导入 image_to_string 时出现 Python 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14640509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:01:34  来源:igfitidea点击:

Python error when importing image_to_string from tesseract

pythonocrtesseract

提问by digital_alchemy

I recently used tesseract OCR with python and I kept getting an error when I was trying to import image_to_stringfrom tesseract.

我最近在 python 中使用了 tesseract OCR,当我尝试image_to_string从 tesseract导入时,我一直收到错误消息。

Code causing the problem:

导致问题的代码:

# Perform OCR using tesseract-ocr library
from tesseract import image_to_string
image = Image.open('input-NEAREST.tif')
print image_to_string(image)

Error caused by above code:

上面代码导致的错误:

Traceback (most recent call last):  
file "./captcha.py", line 52, in <module>  
from tesseract import image_to_string  
ImportError: cannot import name image_to_string

I've verified that the tesseract module is installed:

我已经验证了 tesseract 模块已安装:

digital_alchemy@roaming-gnome /home $ pydoc modules | grep 'tesseract'
Hdf5StubImagePlugin _tesseract          gzip                sipconfig
ORBit               cairo               mako                tesseract

I believe that I've grabbed all the required packages but unfortunately I'm just stuck at this point. It appears that the function is not in the module.

我相信我已经获取了所有必需的包,但不幸的是我只是卡在了这一点上。该功能似乎不在模块中。

Any help greatly appreciated.

非常感谢任何帮助。

回答by m.brindley

Is your syntax correct for the module you have installed? That image_to_stringfunctions looks like it is from PyTesser per the usage example on this page: https://code.google.com/p/pytesser/

您安装的模块的语法是否正确?这个image_to_string功能看起来是从PyTesser每使用例如,此网页上: https://code.google.com/p/pytesser/

Your import looks like it is for python-tesseract which has a more complicated usage example listed: https://code.google.com/p/python-tesseract/

您的导入看起来像是针对 python-tesseract 的,其中列出了更复杂的用法示例:https: //code.google.com/p/python-tesseract/

回答by Logan

Another possibility that seems to have worked for me is to modify pytesseract so that instead of import Image it has from PIL import Image

另一种似乎对我有用的可能性是修改 pytesseract 以便它不是 import Image 它具有 from PIL import Image

Code that works in PyCharm after modifying pytesseract:

修改pytesseract后在PyCharm中工作的代码:

from pytesseract import image_to_string
from PIL import Image

im = Image.open(r'C:\Users\<user>\Downloads\dashboard-test.jpeg')
print(im)

print(image_to_string(im))

Pytesseract I installed via the package management built into PyCharm

我通过 PyCharm 内置的包管理安装了 Pytesseract

回答by Saurabh

For windows followed below steps

对于 Windows 遵循以下步骤

pip3 install pytesseract 
pip3 install pillow

Installation of tessaract-ocr is also required https://github.com/tesseract-ocr/tesseract/wikiotherwise you will get an error Tessract is not on path

还需要安装 tessaract-ocr https://github.com/tesseract-ocr/tesseract/wiki否则你会得到一个错误 Tessract is not on path

Python code

Python代码

from PIL import Image
from pytesseract import image_to_string

print ( image_to_string(Image.open('test.tif'),lang='eng')  )

回答by Ahron Moshe

what works for me:

什么对我有用:

after I install the pytesseract form tesseract-ocr-setup-3.05.02-20180621.exeI add the line pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"and use the code form the above this is all the code:

在我安装 pytesseract 表单tesseract-ocr-setup-3.05.02-20180621.exe 后,我添加了该行 pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"并使用上面的代码表单,这是所有代码:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd="C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
im=Image.open("C:\Users\<user>\Desktop\ro\capt.png")
print(pytesseract.image_to_string(im,lang='eng'))

I am using windows 10 with PyCharm Community Edition 2018.2.3 x64

我正在使用 Windows 10 和 PyCharm 社区版 2018.2.3 x64