我创建了一个将图像打印为文本的程序。
环境变量
变量名称:pytesseract
变量值:pytesseract.pytesseract.tesseract_cmd =
r'C:\ Program Files(x86)\ Tesseract-OCR \ tesseract.exe'
//码
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd= r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
image1 = Image.open("C:\python\program\image.png")
print(pytesseract.image_to_string(image1))
错误:
Traceback (most recent call last):
File "C:/python/program/Image_OCR.py", line 13, in <module>
print(pytesseract.image_to_string(image1))
File "C:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 338, in image_to_string
}[output_type]()
File "C:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 337, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 246, in run_and_get_output
run_tesseract(**kwargs)
File "C:\python\venv\lib\site-packages\pytesseract\pytesseract.py", line 222, in run_tesseract
raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files (x86)\\Tesseract-OCR\\tessdata/eng.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory. Failed loading language \'eng\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')
实际结果:将图像打印为文本。
最佳答案
我有同样的问题。我正在使用Ubuntu。我评论了下面的行pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract'
它为我工作。
尝试删除/注释行pytesseract.pytesseract.tesseract_cmd= r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe
关于python - 如何解决“PATH”问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56512723/