- 安装 Tesseract OCR 引擎本身:这是一个独立的程序,由 Google 维护,Python 只是调用它。
- 安装 Python 的 Tesseract 绑定库:这是一个 Python 包,它让 Python 代码能够与 Tesseract 引擎进行通信。
第 1 步:安装 Tesseract OCR 引擎
这是最关键的一步,如果你跳过它,Python 库将无法工作,你需要根据你的操作系统来选择对应的安装方法。

Windows
-
下载安装程序: 访问 Tesseract at UB Mannheim 官方页面,这是目前维护得最好的 Windows 版本下载地址: https://github.com/UB-Mannheim/tesseract/wiki
-
运行安装程序:
- 下载
tesseract-ocr-w64-setup-v5.3.3.20251005.exe(版本号可能更新)。 - 以管理员身份运行安装程序。
- 重要:在安装过程中,请务必勾选 "Additional language data" 选项,否则你将无法识别任何文字,你可以选择所有语言,或者只勾选你需要的语言(如中文
Chinese_Simp)。
- 下载
-
记住安装路径:
- 默认安装路径是
C:\Program Files\Tesseract-OCR。 - 安装程序会自动将此路径添加到系统的环境变量
Path中,所以你通常不需要手动配置。
- 默认安装路径是
-
验证安装:
(图片来源网络,侵删)- 打开命令提示符 或 PowerShell。
- 输入
tesseract --version,如果看到版本信息,说明安装成功。 - 输入
tesseract --list-langs,你会看到刚才安装的语言列表。
macOS
使用 Homebrew 是最简单的方法。
-
安装 Tesseract: 打开终端,运行以下命令,这会安装 Tesseract 引擎以及最常用的语言数据包(包括英文)。
brew install tesseract
-
安装其他语言数据: 如果你需要其他语言(如中文),需要单独安装。
# 安装简体中文 brew install tesseract-lang # 或者安装所有语言 brew install tesseract-lang
-
验证安装:
(图片来源网络,侵删)tesseract --version tesseract --list-langs
Linux (以 Ubuntu/Debian 为例)
使用 apt 包管理器。
-
安装 Tesseract 和语言包: 打开终端,运行以下命令。
# 更新包列表 sudo apt update # 安装 tesseract-ocr 和英文语言包 sudo apt install tesseract-ocr # 安装简体中文语言包 sudo apt install tesseract-ocr-chi-sim
-
验证安装:
tesseract --version tesseract --list-langs
第 2 步:安装 Python 绑定库
Tesseract 引擎已经准备好了,我们安装 Python 的库来调用它,最常用的是 pytesseract。
-
安装
pytesseract: 打开你的终端或命令提示符,运行 pip 命令。pip install pytesseract
-
安装 Pillow 库:
pytesseract通常需要Pillow库来处理图像文件,如果你还没有安装,最好也一并安装。pip install Pillow
第 3 步:Python 代码示例与常见问题
安装完成后,你就可以在 Python 中使用 Tesseract 了。
基本示例
准备一张包含文字的图片,image.png。
import pytesseract
from PIL import Image
# --- Tesseract 没有添加到系统环境变量 ---
# 你需要手动指定 tesseract.exe 的路径
# Windows 默认路径:
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# 打开图片文件
image_path = 'image.png' # 替换成你的图片路径
try:
# 使用 pytesseract 进行 OCR 识别
text = pytesseract.image_to_string(Image.open(image_path))
# 打印识别出的文本
print("识别结果:")
print(text)
except FileNotFoundError:
print(f"错误:找不到图片文件 '{image_path}',请检查路径是否正确。")
except pytesseract.pytesseract.TesseractNotFoundError:
print("错误:找不到 Tesseract OCR 引擎。")
print("请确保:")
print("1. Tesseract OCR 已正确安装。")
print("2. Tesseract 的安装目录已添加到系统的 PATH 环境变量中。")
print("3. 如果不想配置环境变量,请在代码中指定 tesseract_cmd 的路径。")
except Exception as e:
print(f"发生未知错误: {e}")
常见问题与解决方案
问题 1:TesseractNotFoundError 错误
- 原因:Python 找不到 Tesseract 引擎。
- 解决方案:
- 检查安装:确保第 1 步的 Tesseract 引擎已经成功安装,在命令行运行
tesseract --version验证。 - 检查环境变量:确保 Tesseract 的安装路径(如
C:\Program Files\Tesseract-OCR)在系统的Path环境变量中,重启你的 IDE 或命令行后再试。 - 在代码中指定路径(推荐用于快速调试):在代码开头加上下面这行,把路径改成你自己的。
import pytesseract # Windows 示例 pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # macOS / Linux 示例 (通常不需要,因为 brew/apt 会自动配置路径) # pytesseract.pytesseract.tesseract_cmd = '/usr/local/bin/tesseract'
- 检查安装:确保第 1 步的 Tesseract 引擎已经成功安装,在命令行运行
问题 2:识别不出中文,或者识别出乱码
- 原因:Tesseract 引擎没有安装对应语言的数据包。
- 解决方案:
- 检查语言包:在命令行运行
tesseract --list-langs,确认列表中是否有chi_sim(简体中文) 或chi_tra(繁体中文)。 - 重新安装语言包:如果列表中没有,请返回第 1 步,根据你的操作系统重新安装对应语言包,Windows 用户需要重新运行安装程序并勾选语言。
- 检查语言包:在命令行运行
问题 3:识别准确率不高
- 原因:OCR 效果受多种因素影响,如图片分辨率、模糊度、背景复杂度、字体等。
- 解决方案:
- 预处理图片:在使用
pytesseract之前,使用Pillow或OpenCV对图片进行预处理,通常能显著提高准确率。- 转为灰度图:
img = img.convert('L') - 二值化:将图片转为黑白两色,去除背景干扰。
- 降噪:去除图片中的噪点。
- 提高分辨率:确保图片清晰,DPI 足够高。
- 转为灰度图:
- 使用更高级的参数:
image_to_string函数可以传入参数来优化识别。# 示例:指定语言,并使用 LSTM 引擎 text = pytesseract.image_to_string( Image.open('image.png'), lang='chi_sim+eng', # 同时识别中文和英文 config='--psm 6' # PSM (Page Segmentation Mode) 设置页面分割模式 )常用的 PSM 模式:
--psm 6: 假设统一的文本块。--psm 3: 默认,全自动页面分割,但效果不一定最好。--psm 1: 将图片当作单个文本行。
- 预处理图片:在使用
- 安装引擎:根据你的操作系统,安装 Tesseract OCR 引擎本身,务必安装所需的语言包。
- 安装库:使用
pip install pytesseract Pillow安装 Python 库。 - 验证路径:确保 Python 能找到 Tesseract 引擎(通过环境变量或代码中的
tesseract_cmd)。 - 编写代码:使用
pytesseract.image_to_string()进行识别,并对图片进行预处理以提高准确率。
按照以上步骤,你应该就能成功在 Python 中使用 Tesseract OCR 了。
