杰瑞科技汇

Python pytesser怎么下载?

pytesser 项目已经不再积极维护,并且它依赖的 Tesseract OCR 引擎版本非常老旧,对于新项目,强烈推荐使用更现代、更强大的 pytesseract 库。

Python pytesser怎么下载?-图1
(图片来源网络,侵删)

下面我会先解释 pytesser 的安装方法,然后强烈建议你转向 pytesseract,并提供 pytesseract 的完整安装和使用教程。


Part 1: pytesser 的安装与使用(不推荐)

pytesser 实际上是 Tesseract OCR 引擎的一个 Python 封装器,它的安装过程比 pytesseract 稍微复杂一些。

安装 Tesseract OCR 引擎

pytesser 本身不包含 OCR 引擎,它需要你先安装系统级的 Tesseract 程序。

Windows:

Python pytesser怎么下载?-图2
(图片来源网络,侵删)
  1. 访问 Tesseract 官方 GitHub 的发布页面:https://github.com/UB-Mannheim/tesseract/wiki
  2. 下载最新的安装程序(tesseract-ocr-w64-setup-5.3.0.20251214.exe)。
  3. 运行安装程序。在安装过程中,请务必勾选 "Additional language data" 选项,并选择你需要的语言包(如 chi_sim 简体中文,eng 英文等)。
  4. 记住安装路径,通常是 C:\Program Files\Tesseract-OCR

macOS (使用 Homebrew):

brew install tesseract
# 安装中文语言包
brew install tesseract-lang

Linux (Debian/Ubuntu):

sudo apt update
sudo apt install tesseract-ocr
# 安装中文语言包
sudo apt install tesseract-ocr-chi-sim

安装 pytesser

pytesser 在 PyPI 上,所以你可以用 pip 安装。

pip install pytesser

配置环境变量(关键步骤)

pytesser 需要知道 Tesseract OCR 引擎的 tesseract.exe 文件在哪里,你可以通过设置环境变量 TESSDATA_PREFIX 来指定。

Python pytesser怎么下载?-图3
(图片来源网络,侵删)

Windows:

  1. 右键点击 "此电脑" -> "属性" -> "高级系统设置" -> "环境变量"。
  2. 在 "系统变量" 部分,点击 "新建"。
  3. 变量名: TESSDATA_PREFIX
  4. 变量值: Tesseract 的安装路径。C:\Program Files\Tesseract-OCR
  5. 点击确定保存所有设置。

macOS / Linux: 通常在安装时 Tesseract 的路径会被自动配置好,如果遇到问题,可以在你的 shell 配置文件(如 ~/.bashrc~/.zshrc)中添加:

export TESSDATA_PREFIX=/usr/share/tesseract-ocr/4.00/tessdata

然后运行 source ~/.bashrcsource ~/.zshrc 使其生效。

一个简单的 pytesser 示例

from pytesser import *
# 确保你有一张名为 'image.png' 的图片在脚本同目录下
try:
    text = image_to_string('image.png')
    print("识别结果:")
    print(text)
except Exception as e:
    print(f"发生错误: {e}")
    print("请确保 Tesseract OCR 已正确安装并配置了环境变量。")

Part 2: 强烈推荐使用 pytesseract(现代、高效)

pytesseractpytesser 的一个现代化分支,它更稳定、更容易安装,并且与最新的 Tesseract OCR 版本兼容得更好。

安装 Tesseract OCR 引擎

这一步和上面完全一样,请确保你已经安装了 Tesseract OCR,并且安装了你需要的语言包。

安装 pytesseractPillow

pytesseract 本身只提供接口,你需要 Pillow 库来处理图像文件。

pip install pytesseract
pip install Pillow

配置 pytesseract 的路径

Windows: 这是最关键的一步。pytesseract 需要知道 tesseract.exe 的位置,最推荐的方法是在代码中直接指定路径。

import pytesseract
from PIL import Image
# 指定你的 tesseract.exe 文件路径
# 注意路径中的反斜杠需要用双反斜杠 \\ 或者原始字符串 r''
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# 打开图片
image = Image.open('image.png')
# 进行 OCR 识别
text = pytesseract.image_to_string(image)
print("识别结果:")
print(text)

macOS / Linux: 通常不需要额外配置,只要 Tesseract 通过包管理器正确安装,pytesseract 就能找到它。

pytesseract 的强大功能

pytesseract 提供了很多有用的函数:

  • image_to_string(): 从图像中提取文本。
  • image_to_data(): 提取更详细的信息,包括每个文本块的边界框、置信度等。
  • image_to_pdf_or_hocr(): 将识别结果输出为 PDF 或 hOCR 格式。

示例:获取详细的位置和置信度信息

import pytesseract
from PIL import Image
# 指定 tesseract 路径 (Windows)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
image = Image.open('image_with_text.png')
# 使用 image_to_data 获取详细信息
data = pytesseract.image_to_data(image)
print("详细识别数据:")
print(data)
# 你可以解析这些数据来获取每个单词的位置和置信度
# 打印出置信度高于 80 的单词
for i, line in enumerate(data.splitlines()):
    if i == 0:
        continue  # 跳过表头
    cols = line.split('\t')
    if len(cols) == 12:
        # [level, page_num, block_num, par_num, line_num, word_num, left, top, width, height, conf, text]
        conf = int(cols[10])
        text = cols[11]
        if conf > 80:
            print(f"单词: '{text}', 置信度: {conf}%")

总结与对比

特性 pytesser (已过时) pytesseract (推荐)
维护状态 已停止维护,依赖旧版 Tesseract 积极维护,支持最新 Tesseract 版本
安装复杂度 高,需要手动配置 TESSDATA_PREFIX 环境变量 低,只需在代码中指定 tesseract_cmd (Windows)
依赖 Tesseract OCR Tesseract OCR + Pillow
功能 基础文本识别 基础识别 + 详细数据输出 + PDF/hOCR 导出等
社区支持 几乎没有 活跃,文档和示例丰富
推荐度 不推荐,仅用于维护旧项目 强烈推荐,所有新项目的首选

请直接使用 pytesseract,它更简单、更强大、更可靠,是当前 Python 进行 OCR 识别的标准库,虽然初次配置时在 Windows 上需要指定一下 tesseract_cmd 路径,但这比配置系统环境变量要清晰和可控得多。

分享:
扫描分享到社交APP
上一篇
下一篇