Python pytesseract库使用指南:安装与OCR识别实战

听雨眠
听雨眠
发布时间:2026-10-14| 3人浏览过
原创

本文详细介绍Python中pytesseract库的安装配置与使用方法。重点说明需本地安装Tesseract-ocr.exe并添加中文语言包,通过pip安装依赖库。提供完整代码示例,展示如何调用image_to_string接口识别图片文字,包括中英文混合识别及预处理参数设置,帮助开发者快速实现OCR功能。

说明

使用pytesseract库的前提是本地计算机必须已安装Tesseract-ocr.exe程序,二者需配合使用。

安装Tesseract时务必勾选中文语言包,因为默认配置仅支持英文字符识别。

安装命令


pip install pytesseract

实例


import pytesseract
from PIL import Image
text = pytesseract.image_to_string(Image.open(r"d:\Desktop\39DEE621-40EA-4ad1-90CC-79EB51D39347.png"))
print(text)

识别结果输出


Using Tesseract OCR with Python
# import the necessary packages
from PIL import Image
import pytesseract
import ergperse
import cv2
import os
 
# construct the argument parse and parse the arguments
ap = argparse.ArgunentParser()
ap.add_argument("-i", "--image", required-True,
help="path to input image to be OCR'd")
ap.add_argument("-p", "--preprocess", typesstr, default="thresh",
helpe"type of preprocessing to be done")
args = vars (ap.parse_args())

实例扩展:

Python使用pytesseract库识别图片文字

准备

首先需在本地安装Tesseract客户端软件,随后通过包管理器安装pytesseract库。


pip install pytesseract

若环境中未安装PIL(Python Imaging Library),需一并安装,因为后续调用中第一个参数需由Image类返回对象。


pip install PILLOW

实例


import pytesseract
from PIL import Image

img = Image.open("D://aa.jpg")
text = pytesseract.image_to_string(img, lang="chi_sim")
print(text)

到此这篇关于python pytesseract库的实例用法的文章就介绍到这了,更多相关python pytesseract库是什么内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。