Python中pytesseract库的使用以及注意事项

当我们在使用pytesseract库的时候,使用 pip install pytesseract安装完成后,发现它并不能识别出图片内容,并且会抛出异常pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

这是怎么回事呢?今天让我们一探究竟

尝试

使用代码

import pytesseract
from PIL import Image

image = Image.open("./NormalImg.png")
text = pytesseract.image_to_string(image)
print(text)

报错提示:

    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

官方文档

pytesseract官方文档:https://pypi.org/project/pytesseract/

是我们缺少了tesseract程序

tesseract官方Github地址:https://github.com/UB-Mannheim/tesseract

tesseract官方Github说明https://github.com/UB-Mannheim/tesseract/wiki

安装tesseract

下载地址

Tesseract 5.0.0 32位版本:tesseract-ocr-w32-setup-v5.0.0-alpha.20200328.exe (32 bit)

Tesseract 5.0.0 64位版本:tesseract-ocr-w64-setup-v5.0.0-alpha.20200328.exe (64 bit)

新增百度云盘链接:

链接:https://pan.baidu.com/s/1EO5tFmzn1hqY_M679eSBnw

提取码:nyw4

导入tesseract.exe执行文件地址

添加以下导入路径:

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

最终代码:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image = Image.open("./NormalImg.png")
text = pytesseract.image_to_string(image)
print(text)

至此运行代码不会异常,并可以正常读取图片文字内容

总结

pytesseract包依赖于Tesseract执行文件,需要安装Tesseract

当然Tesseract只能识别标准的ASCII字符串,复杂的验证吗就无法使用pytesseract来读取了


欢迎来跟博主讨论Python有关的问题。