跳到主要内容
Python OCR 文字识别:pytesseract 安装配置与使用 | 极客日志
Python AI 算法
Python OCR 文字识别:pytesseract 安装配置与使用 Python OCR 文字识别基于 pytesseract 库与 Tesseract 引擎。需先安装 Tesseract-OCR 及语言包(如 chi_sim)。手动安装步骤包括下载引擎、配置环境变量、指定代码路径。常见问题涵盖引擎未安装、路径错误、中文乱码及识别率低。通过图像预处理(灰度化、二值化)、调整 PSM/OEM 模式及选择合适语言可提升准确率。支持身份证识别、截图提取、验证码及 PDF 转文本等场景,适用于印刷体文字离线识别。
赛博朋克 发布于 2026/3/20 更新于 2026/5/8 8 浏览Python OCR 文字识别:pytesseract 安装配置与使用
pytesseract 是 Python 的 OCR(光学字符识别)库,可以从图片中提取文字。Windows 上使用需要先安装 Tesseract OCR 引擎。
版本要求
pytesseract 依赖 Tesseract OCR 引擎:
组件 推荐版本 Python 版本 说明 pytesseract 0.3.10 3.7+ Python 封装库 Tesseract-OCR 5.x - OCR 识别引擎 中文语言包 chi_sim - 简体中文识别(可选) 英文语言包 eng - 英文识别(默认自带)
注意 :pytesseract 只是封装库,必须先安装 Tesseract OCR 引擎才能使用。
安装中可能遇到的问题
问题 1:Tesseract 引擎未安装
import pytesseract
pytesseract.image_to_string('test.jpg' )
只装了 pytesseract,没装 Tesseract OCR 引擎。
问题 2:路径未配置
import pytesseract
pytesseract.image_to_string('test.jpg' )
Tesseract 安装了,但 Python 找不到,需要手动指定路径。
问题 3:中文识别乱码
text = pytesseract.image_to_string('中文图片.jpg' )
print (text)
没有安装中文语言包 chi_sim.traineddata。
问题 4:识别准确率低
识别结果错误很多,可能是图片质量差、没有预处理。
方式一:手动安装
步骤 1:安装 Tesseract OCR 引擎
下载地址:Tesseract OCR 引擎下载地址
选择最新版本(如 tesseract-ocr-w64-setup-5.3.3.20231005.exe)下载并安装。
安装时注意 :
勾选"Additional language data" → 选择"Chinese - Simplified"(简体中文)
记住安装路径(默认:C:\Program Files\Tesseract-OCR)
步骤 2:配置环境变量(可选)
右键"此电脑" → 属性 → 高级系统设置 → 环境变量
在"系统变量"中找到"Path",点击编辑
新建:C:\Program Files\Tesseract-OCR
确定保存
步骤 3:安装 pytesseract pip install pytesseract pillow
步骤 4:配置 Tesseract 路径 在 Python 代码中指定 Tesseract 路径:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
步骤 5:下载中文语言包(如未安装) 下载 chi_sim.traineddata(简体中文),放到:
C:\Program Files\Tesseract-OCR\tessdata\
验证安装
基础测试 import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
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:身份证识别 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:截图文字提取 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 参数 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' )
常见问题 Q:TesseractNotFoundError 怎么办?
确认安装了 Tesseract OCR 引擎
在代码中指定路径:
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
检查是否安装了 chi_sim 语言包
确认使用了 lang='chi_sim' 参数
检查语言包位置:C:\Program Files\Tesseract-OCR\tessdata\chi_sim.traineddata
图片预处理:灰度化、二值化、去噪
提高图片分辨率(DPI 300 以上)
选择合适的 PSM 模式
图片文字尽量清晰、背景简单
Tesseract 对手写字识别效果较差,建议使用深度学习模型(如 PaddleOCR)。
Tesseract 是 Apache 2.0 开源协议,可免费商用。
Q:pytesseract 和其他 OCR 方案对比?
方案 优点 缺点 适用场景 pytesseract 免费、离线、轻量 识别率一般、手写字差 印刷体文字 百度 OCR API 识别率高、支持手写 收费、需联网、有调用限制 商业项目 PaddleOCR 识别率高、免费 模型大、配置复杂 高精度需求 EasyOCR 多语言支持、简单易用 速度较慢 多语言场景
常用功能
获取文字位置 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