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

Bla*_*air 8 python-tesseract

尝试将简单的测试图像打印到文本时,我收到以下错误.

我已经确认我有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)))
Run Code Online (Sandbox Code Playgroud)

我希望它打印出图像中的单词,但我总是这样:

    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')
Run Code Online (Sandbox Code Playgroud)

cod*_*ody 6

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

因此,您需要安装Tesseract。Windows 版本可用。我选择了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)))
Run Code Online (Sandbox Code Playgroud)

测试输入:

在此处输入图片说明 结果:

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)))
Run Code Online (Sandbox Code Playgroud)