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

时间:2019-02-16 16:24:34

标签: python-tesseract

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

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

我的代码:

    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')

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

 Route::get('/index', 'SiteController@index')->name('home'); 

1 个答案:

答案 0 :(得分:2)

问题在于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)))

测试输入:

enter image description here 结果:

The (quick) [brown] {fox} jumps!
Over the $43,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.