在pytess中运行示例代码

2024-04-24 00:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行Python2.6.6,希望安装pytesseract包。在提取和安装之后,我可以从命令行调用pytesseract。不过,我想在python中运行tesseract。我有以下代码(ocr.py):

try:
     import Image
except ImportError:
     from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('test.png')))
print(pytesseract.image_to_string(Image.open('test-european.jpg'),lang='fra'))

当我使用python ocr.py运行代码时,得到以下输出:

Traceback (most recent call last):
  File "ocr.py", line 6, in <module>
    print(pytesseract.image_to_string(Image.open('test.png')))
  File "/pytesseract-0.1.6/build/lib/pytesseract/pytesseract.py", line 164, in image_to_string
    raise TesseractError(status, errors)
pytesseract.TesseractError: (2, 'Usage: python tesseract.py [-l language] input_file')

test.png和test-european.jpg位于工作目录中。有人能帮我运行这个代码吗? 我试过以下方法:

  1. 将tesseract命令调整为“pytesseract”
  2. 已安装tesseract ocr

任何帮助都是感激的,因为我正在努力解决这个问题几个小时了。


Tags: to代码pytestimageimportstringpng
1条回答
网友
1楼 · 发布于 2024-04-24 00:24:01

tesseract_cmd应该指向命令行程序^{},而不是pytesseract

例如,在Ubuntu上,可以使用以下命令安装程序:

sudo apt install tesseract-ocr

然后将变量设置为tesseract/usr/bin/tesseract

相关问题 更多 >