pytesseract 中英文 识别图片文字

要使用 pytesseract 识别图片文字,你需要先安装 Tesseract OCR引擎 和 Pillow库,然后通过几行 Python 代码导入库、加载图片,并调用 image_to_string() 函数进行识别,传入图片路径和指定语言 (如 'eng' 或 'chi_sim') 即可获得文本内容。

步骤 1: 安装 Tesseract OCR引擎

这是核心部分,需要安装在你的操作系统上,而不是Python库里。

Windows/macOS: 前往 Tesseract-OCR GitHub Releases页面 (或其他官方源) 下载并安装对应版本。

Linux (Debian/Ubuntu): 运行:

bash 复制代码
sudo apt install tesseract-ocr

安装语言包: 如果需要识别中文,同时安装中文语言包,例如在Linux上是:

bash 复制代码
sudo apt install tesseract-ocr-chi-sim
# 或 centos
sudo yum install tesseract-ocr-chi-sim

步骤 2: 安装 Python库

安装 Pillow (PIL): pip install Pillow

bash 复制代码
pip install Pillow

安装 pytesseract: pip install pytesseract

bash 复制代码
pip install pytesseract

步骤 3: 编写 Python代码

python 复制代码
import pytesseract
from PIL import Image

# ----------------------------------------------------------
# Windows用户: 如果Tesseract不在系统PATH中,需要指定其路径
# 例如: pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# ----------------------------------------------------------

def ocr_image_to_text(image_path, language='eng'):
    """
    使用 pytesseract 从图片中提取文字。
    :param image_path: 图片文件路径
    :param language: 识别的语言 (如 'eng' 英文, 'chi_sim' 简体中文)
    :return: 识别出的文字
    """
    try:
        # 1. 使用Pillow打开图片
        img = Image.open(image_path)

        # 2. 使用pytesseract进行OCR识别
        
        # 设置环境变量(只在当前会话中有效)
    		  pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'  # 示例路径
        pytesseract.pytesseract.tessdata_dir_config = r'C:\Program Files\Tesseract-OCR\tessdata'  # 示例路径
      # lang参数指定语言包
      # 或者在调用时直接指定
      text = pytesseract.image_to_string(Image.open(image_path), lang=language, config="C:\Program Files\Tesseract-OCR\\tessdata"))

        return text
    except FileNotFoundError:
        return f"错误: 找不到文件 {image_path}"
    except Exception as e:
        return f"识别时发生错误: {e}"

# --- 示例用法 ---
if __name__ == '__main__':
    # 假设你的图片名为 'example.png' 且在同一目录下
    # 并且安装了中文语言包 'chi_sim'
    image_file = 'example.png' # <-- 替换成你的图片路径

    # 识别英文
    english_text = ocr_image_to_text(image_file, language='eng')
    print("--- 英文识别结果 ---")
    print(english_text)

    # 识别简体中文 (需要安装 'chi_sim' 语言包)
    chinese_text = ocr_image_to_text(image_file, language='chi_sim')
    print("\n--- 中文识别结果 ---")
    print(chinese_text)

    # 如果是混合语言,可以尝试 'eng+chi_sim'
    mixed_text = ocr_image_to_text(image_file, language='eng+chi_sim')
    print("\n--- 混合语言识别结果 ---")
    print(mixed_text)

步骤 4、报错提示找不到chi_sim语言

请下载:https://github.com/tesseract-ocr/tessdata,把 chi_sim.traineddata 下载后的文件放在 C:\Program Files\Tesseract-OCR\tessdata 文件夹下面。然后运行完整代码,可正常识别中文文字。

相关推荐
知行合一。。。24 分钟前
Python--04--数据容器(总结)
开发语言·python
架构师老Y28 分钟前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange1 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium271 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499991 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉1 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi2 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^2 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool
宝贝儿好2 小时前
【LLM】第二章:文本表示:词袋模型、小案例:基于文本的推荐系统(酒店推荐)
人工智能·python·深度学习·神经网络·自然语言处理·机器人·语音识别
王夏奇2 小时前
pythonUI界面弹窗设置的几种办法
python·ui