import pytesseract
from PIL import Image
image = Image.open('image.png')
print(pytesseract.image_to_string(image))
第一次通过pytesseract调用tesseract时,运行后报错:
- Traceback(mostrecentcalllast):
- File "d:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 260, in get_tesseract_version
- [tesseract_cmd, '--version'], stderr=subprocess.STDOUT
- .....(部分省略)
- pytesseract.pytesseract.TesseractNotFoundError: tesseract.exe is not installed or it's not in your path
根据第4行报错提示,初步怀疑是环境变量的原因
于是检查了本机环境变量的设置,发现都设置无误
再进行判断查看了第2、3行的报错信息:tesseract_cmd --version
于是进入python包lib\site-packages\pytesseract\ 修改了pytesseract.py里面的这段源码:
numpy_installed = find_loader('numpy') is not None
if numpy_installed:
from numpy import ndarray
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'
RGB_MODE = 'RGB'
OSD_KEYS = {
修改为下面这样:
numpy_installed = find_loader('numpy') is not None
if numpy_installed:
from numpy import ndarray
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
#tesseract_cmd = 'tesseract'
tesseract_cmd = r'D:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
RGB_MODE = 'RGB'
OSD_KEYS = {
在此运行,能正常识别图片的文字。