在python脚本中使用光学字符识别

时间:2018-03-08 21:03:29

标签: python ocr valueerror pytesser

我想完成看似简单的运行python脚本的任务,该脚本使用OCR从图像中提供一串文本。我的代码:

from PIL import Image
from pytesseract import *

image_file = 'IMG_9296'
im = Image.open(image_file)
text = image_to_string(im)
text = image_file_to_string(image_file)
text = image_file_to_string(image_file, graceful_errors=True)
print "=====output=======\n"
print text

但是,我在第二行遇到了麻烦。我在Mac OS X上成功安装了枕头,PIL和pytesseract(运行Python 2.7,Pillow-5.0.0 pytesseract-0.2.0)。但是我收到了这个错误:

Traceback (most recent call last):
File "./HQcode2.py", line 2, in <module>
from pytesseract import *
File "/Library/Python/2.7/site-packages/pytesseract/__init__.py", line 
1, in <module>
from .pytesseract import (
File "/Library/Python/2.7/site-packages/pytesseract/pytesseract.py", 
line 9, in <module>
import Image
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 27, in 
<module>
from . import VERSION, PILLOW_VERSION, _plugins
ValueError: Attempted relative import in non-package

我查了一下这个错误但是还没有发现任何对我的情况有帮助的东西。这里似乎有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

你能确认你也安装了tesseract吗? (https://github.com/madmaze/pytesseract#installation

基于此问题:https://github.com/madmaze/pytesseract/issues/58,该软件包的作者似乎表明您的问题是缺少依赖项。