跳到主要内容
Python OCR 文字识别:pytesseract 安装配置与实战 | 极客日志
Python AI 算法
Python OCR 文字识别:pytesseract 安装配置与实战 综述由AI生成 pytesseract 是 Python 中常用的 OCR 库,依赖 Tesseract 引擎。详细讲解了 Windows 下的手动安装流程,包括引擎下载、环境变量配置、语言包添加及路径指定。内容涵盖常见报错排查、图像预处理技巧、中英文识别实战案例(身份证、截图、验证码、PDF 转换),以及获取文字位置、置信度检测等进阶用法。同时对比了不同 OCR 方案的优缺点,帮助开发者根据场景选择合适方案。
林间仙子 发布于 2026/3/26 更新于 2026/4/25 1 浏览Python OCR 文字识别:pytesseract 安装配置与实战
pytesseract 是 Python 中常用的光学字符识别(OCR)库,它封装了 Tesseract OCR 引擎。要在 Windows 上顺利使用,核心在于正确安装 Tesseract 引擎并配置好环境变量。
版本要求
pytesseract 本身只是一个 Python 封装库,必须依赖底层的 Tesseract OCR 引擎才能工作。
组件 推荐版本 Python 版本 说明 pytesseract 0.3.10+ 3.7+ Python 封装库 Tesseract-OCR 5.x - OCR 识别引擎 中文语言包 chi_sim - 简体中文识别(可选) 英文语言包 eng - 英文识别(默认自带)
注意 :只安装 pytesseract 是不够的,必须先安装 Tesseract OCR 引擎。
常见安装问题排查
在实际使用中,新手常遇到以下几类错误,提前了解能节省不少调试时间。
1. Tesseract 引擎未安装
报错信息通常包含 TesseractNotFoundError: tesseract is not installed。这说明系统里根本没有 Tesseract 程序,或者 Python 找不到它。
2. 路径未配置
即使安装了 Tesseract,如果没加入系统 PATH,Python 依然无法调用。此时需要手动指定 tesseract_cmd 的路径。
3. 中文识别乱码
如果图片包含中文但输出乱码或空白,通常是缺少 chi_sim.traineddata 语言包。需单独下载并放入 tessdata 目录。
4. 识别准确率低
识别结果错误多,往往不是库的问题,而是图片质量差、对比度低或未做预处理。建议先对图像进行灰度化、二值化处理。
手动安装指南
虽然市面上有一些一键安装工具,但掌握手动安装流程更能理解底层原理,也方便后续维护。
步骤一:安装 Tesseract OCR 引擎
从 GitHub 官方仓库下载最新安装包(如 tesseract-ocr-w64-setup-5.3.3.exe)。
安装时请注意勾选 "Additional language data" ,并在列表中选择 "Chinese (Simplified)" 。这一步至关重要,否则无法识别中文。
记住安装路径,默认为 C:\Program Files\Tesseract-OCR。
步骤二:配置环境变量(可选)
为了在命令行直接调用,可以将 Tesseract 路径添加到系统环境变量中:
右键'此电脑' → 属性 → 高级系统设置 → 环境变量。
在'系统变量'中找到 Path,点击编辑。
新建条目:C:\Program Files\Tesseract-OCR。
保存退出。
步骤三:安装 Python 依赖 通过 pip 安装 pytesseract 和图像处理库 Pillow:
pip install pytesseract pillow
步骤四:在代码中指定路径 如果你没有配置环境变量,必须在代码中显式指定 Tesseract 的可执行文件路径:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
步骤五:补充中文语言包 如果安装时未勾选中文,需手动下载 chi_sim.traineddata 文件,放置到以下目录:
C:\Program Files\Tesseract-OCR\tessdata\
验证安装 安装完成后,运行一段简单的测试代码确认环境是否就绪。
import pytesseract
from PIL import Image
img = Image.open ('test_eng.jpg' )
text = pytesseract.image_to_string(img)
print (f"英文识别结果:\n{text} " )
img_cn = Image.open ('test_chn.jpg' )
text_cn = pytesseract.image_to_string(img_cn, lang='chi_sim' )
print (f"中文识别结果:\n{text_cn} " )
import pytesseract
print (pytesseract.get_languages())
实用案例
案例 1:身份证信息提取 身份证识别通常需要结合 OpenCV 进行预处理,以提高准确率。
import pytesseract
from PIL import Image
import cv2
img = cv2.imread('身份证.jpg' )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5 , 5 ), 0 )
_, binary = cv2.threshold(blur, 0 , 255 , cv2.THRESH_BINARY + cv2.THRESH_OTSU)
text = pytesseract.image_to_string(binary, lang='chi_sim' )
print (f"身份证信息:\n{text} " )
lines = text.split('\n' )
for line in lines:
if '姓名' in line:
print (f"姓名:{line.split('姓名' )[-1 ].strip()} " )
if '身份证号' in line:
print (f"身份证号:{line.split('身份证号' )[-1 ].strip()} " )
案例 2:截图文字提取 利用 ImageGrab 截取屏幕内容并进行识别。
import pytesseract
from PIL import ImageGrab
screenshot = ImageGrab.grab()
screenshot.save('screenshot.png' )
text = pytesseract.image_to_string(screenshot, lang='chi_sim+eng' )
print (f"截图文字:\n{text} " )
with open ('extracted_text.txt' , 'w' , encoding='utf-8' ) as f:
f.write(text)
案例 3:验证码识别 import pytesseract
from PIL import Image
import cv2
import numpy as np
img = cv2.imread('captcha.jpg' )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 150 , 255 , cv2.THRESH_BINARY)
kernel = np.ones((2 , 2 ), np.uint8)
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
text = pytesseract.image_to_string(opening, config=custom_config)
print (f"验证码:{text.strip()} " )
案例 4:批量 PDF 转文字 import pytesseract
from pdf2image import convert_from_path
from PIL import Image
def pdf_to_text (pdf_path, output_txt ):
images = convert_from_path(pdf_path, dpi=300 )
full_text = ""
for i, img in enumerate (images):
print (f"处理第{i+1 } 页..." )
text = pytesseract.image_to_string(img, lang='chi_sim+eng' )
full_text += f"\n========== 第{i+1 } 页 ==========\n"
full_text += text
with open (output_txt, 'w' , encoding='utf-8' ) as f:
f.write(full_text)
print (f"完成!文本已保存到 {output_txt} " )
提高识别准确率
1. 图像预处理 清晰的图像是识别的基础。常见的预处理手段包括灰度化、二值化和去噪。
import cv2
img = cv2.imread('test.jpg' )
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 127 , 255 , cv2.THRESH_BINARY)
denoised = cv2.fastNlMeansDenoising(gray, None , 10 , 7 , 21 )
resized = cv2.resize(gray, None , fx=2 , fy=2 , interpolation=cv2.INTER_CUBIC)
2. 配置 OCR 参数 Tesseract 支持丰富的配置项,例如页面分割模式(PSM)和引擎模式(OEM)。
import pytesseract
text = pytesseract.image_to_string(img, lang='chi_sim' , config='--psm 7' )
text = pytesseract.image_to_string(img, config='--psm 6 -c tessedit_char_whitelist=0123456789' )
3. 选择合适的语言
text = pytesseract.image_to_string(img, lang='eng' )
text = pytesseract.image_to_string(img, lang='chi_sim' )
text = pytesseract.image_to_string(img, lang='chi_sim+eng' )
text = pytesseract.image_to_string(img, lang='chi_tra' )
常见问题 FAQ Q:TesseractNotFoundError 怎么办?
确认已安装 Tesseract 引擎,并在代码中通过 tesseract_cmd 指定绝对路径。
Q:中文识别全是乱码?
检查是否安装了 chi_sim 语言包,确认使用了 lang='chi_sim' 参数,并验证 .traineddata 文件是否在 tessdata 目录下。
Q:识别准确率很低怎么办?
尝试优化图片质量(提高 DPI 至 300 以上),增加预处理步骤(去噪、二值化),或调整 PSM 模式。
Q:能识别手写字吗?
Tesseract 对手写体效果较差,建议改用深度学习模型如 PaddleOCR。
Q:商业使用需要付费吗?
Tesseract 采用 Apache 2.0 开源协议,可免费商用。
Q:pytesseract 和其他 OCR 方案对比?
方案 优点 缺点 适用场景 pytesseract 免费、离线、轻量 识别率一般、手写字差 印刷体文字 百度 OCR API 识别率高、支持手写 收费、需联网 商业项目 PaddleOCR 识别率高、免费 模型大、配置复杂 高精度需求 EasyOCR 多语言支持、易用 速度较慢 多语言场景
常用功能扩展
获取文字位置 如果需要定位文字在图片中的坐标,可以使用 image_to_data。
import pytesseract
from PIL import Image
img = Image.open ('test.jpg' )
data = pytesseract.image_to_data(img, lang='chi_sim' , output_type=pytesseract.Output.DICT)
for i, text in enumerate (data['text' ]):
if text.strip():
x, y, w, h = data['left' ][i], data['top' ][i], data['width' ][i], data['height' ][i]
print (f"文字:{text} , 位置:({x} , {y} ), 大小:{w} x{h} " )
保存为 PDF import pytesseract
from PIL import Image
img = Image.open ('scan.jpg' )
pdf = pytesseract.image_to_pdf_or_hocr(img, lang='chi_sim' , extension='pdf' )
with open ('output.pdf' , 'wb' ) as f:
f.write(pdf)
置信度检测 import pytesseract
from PIL import Image
img = Image.open ('test.jpg' )
data = pytesseract.image_to_data(img, lang='chi_sim' , output_type=pytesseract.Output.DICT)
for i, text in enumerate (data['text' ]):
confidence = data['conf' ][i]
if confidence != -1 and text.strip():
print (f"文字:{text} , 置信度:{confidence} %" )
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
RSA密钥对生成器 生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
Mermaid 预览与可视化编辑 基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
随机西班牙地址生成器 随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online
Gemini 图片去水印 基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
curl 转代码 解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online