【发布时间】:2018-03-09 22:35:53
【问题描述】:
我正在尝试在我的 python 脚本中使用 pytesseract 来读出图像中的一串文本,但我不断收到错误。我现在正在尝试这段代码:
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files
(x86)\\Tesseract-OCR\\tesseract'
# Include the above line, if you don't have tesseract executable in
#your PATH
# Example tesseract_cmd: 'C:\\Program Files (x86)\\Tesseract-
#OCR\\tesseract'
# Simple image to string
print(pytesseract.image_to_string(Image.open('IMG_9296.jpg')))
IMG_9296.jpg 文件位于我的桌面上:~/Desktop。我已经安装了 tesseract 和 pytesseract,因为在我的命令行中输入 tesseract 会返回有关它的信息。
当我在命令行中输入pip install pytesseract 时,我会返回:
Requirement already satisfied: pytesseract in /Library/Python/2.7/site-packages
Requirement already satisfied: Pillow in /Library/Python/2.7/site-packages (from pytesseract)
所以我猜/Library/Python/2.7/site-packages 是我通向 pytesseract 的路径,所以我尝试将它放入 pytesseract.pytesseract.tesseract_cmd,但没有奏效。无论哪种方式,我都会收到此错误(我认为来自import pytesseract 行):
ValueError: Attempted relative import in non-package
我是否需要进入不同的路径,或者在某处移动/复制 pytesseract?我不太确定发生了什么。
【问题讨论】:
-
PIL 和枕头不同 - 也许您的导入错误在 PIL 上?
标签: python tesseract valueerror python-tesseract