Pytesseract.TesseractError '用法:python pytesseract.py [-l lang] input_file

Pytesseract.TesseractError 'Usage: python pytesseract.py [-l lang] input_file

尝试将简单的测试图像打印为文本时出现以下错误。

我已确认我有 Pillow (PIL 1.1.7) 并尝试卸载并重新安装 pytesseract。文件路径是正确的,因为如果我更改它们,我会收到另一个错误,提示找不到该文件。

我的代码:

    from PIL import Image
    import pytesseract

    pytesseract.pytesseract.tesseract_cmd= r'C:\Users\bbrown2\AppData\Local\
    Programs\Python\Python37\Scripts\pytesseract'

    img = r'C:\Users\bbrown2\Desktop\test.png'

    print(pytesseract.image_to_string(Image.open(img)))

我希望它能打印出图像中的文字,但我总是得到这个:

    Traceback (most recent call last):
    File 
   "c:\Users\bbrown2\Desktop\PythonMaterials\python_test_tesseract.py", line 
    14, in <module>
   print(pytesseract.image_to_string(Image.open(image)))
   File "C:\Users\bbrown2\AppData\Local\Programs\Python\Python37\lib\site- 
   packages\pytesseract\pytesseract.py", line 309, in image_to_string
   }[output_type]()
    File "C:\Users\bbrown2\AppData\Local\Programs\Python\Python37\lib\site- 
   packages\pytesseract\pytesseract.py", line 308, in <lambda>
   Output.STRING: lambda: run_and_get_output(*args),
   File "C:\Users\bbrown2\AppData\Local\Programs\Python\Python37\lib\site- 
   packages\pytesseract\pytesseract.py", line 218, in run_and_get_output
   run_tesseract(**kwargs)
   File "C:\Users\bbrown2\AppData\Local\Programs\Python\Python37\lib\site- 
   packages\pytesseract\pytesseract.py", line 194, in run_tesseract
   raise TesseractError(status_code, get_errors(error_string))
   pytesseract.pytesseract.TesseractError: (2, 'Usage: python pytesseract.py 
   [-l lang] input_file')

问题是 pytesseract 只是命令行程序 Tesseract 的一个很好的 Python 包装器。 您应该将 tesseract_cmd 指向实际的 Tesseract 二进制文件,而不是 pytesseract CLI 实用程序。

因此,您需要安装 Tesseract。 Windows builds 可用。我选择的是3.05版本的安装器,默认安装到C:\Program Files (x86)\Tesseract-OCR\tesseract。然后,我 运行 以下内容运行良好:

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = (
    r"C:\Program Files (x86)\Tesseract-OCR\tesseract"
)

img = r"C:\Users\cody\Desktop\ocrtest.png"

print(pytesseract.image_to_string(Image.open(img)))

测试输入:

结果:

The (quick) [brown] {fox} jumps!
Over the ,456.78 <lazy> #90 dog
& duck/goose, as 12.5% of E-mail
from aspammer@website.com is spam.
Der ,,schnelle” braune Fuchs springt
fiber den faulen Hund. Le renard brun
«rapide» saute par-dessus le chien
paresseux. La volpe marrone rapida
salta sopra i] cane pigro. El zorro
marrén répido salta sobre el perro
perezoso. A raposa marrom répida
salta sobre 0 C50 preguicoso.