import pytesseract
from PIL import Image

image = Image.open('image.png')
print(pytesseract.image_to_string(image))

第一次通过pytesseract调用tesseract时,运行后报错:

  1. Traceback(mostrecentcalllast):
  2. File "d:\Python36\lib\site-packages\pytesseract\pytesseract.py", line 260, in get_tesseract_version
  3. [tesseract_cmd, '--version'], stderr=subprocess.STDOUT 
  4. .....(部分省略)
  5. 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 = {

在此运行,能正常识别图片的文字。

GitHub 加速计划 / te / tesseract
60.1 K
9.29 K
下载
tesseract-ocr/tesseract: 是一个开源的光学字符识别(OCR)引擎,适用于从图像中提取和识别文本。特点是可以识别多种语言,具有较高的识别准确率,并且支持命令行和API调用。
最近提交(Master分支:2 个月前 )
bc490ea7 Don't check for a directory, because a symbolic link is also allowed. Signed-off-by: Stefan Weil <sw@weilnetz.de> 4 个月前
2991d36a - 4 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐