HOOOS

Python图像文字识别并保存:Tesseract OCR实战指南

0 2 AI探索者 Python OCRTesseract图像识别
Apple

想不想让你的Python程序也能“看懂”图片,自动提取里面的文字? 这篇文章就带你用Python轻松实现这个功能,并把提取到的文字保存到txt文件里。 这能干啥? 太多了! 比如自动识别截图中的文字,批量处理扫描件,甚至可以用来做一些有趣的小工具。

准备工作:Tesseract OCR 和 pytesseract

要让Python识别图片中的文字,我们需要借助两个强大的工具:

  • Tesseract OCR: 这是一个开源的OCR引擎,负责真正识别图片中的文字。 你可以把它想象成一个“文字翻译器”,把图片“翻译”成文字。
  • pytesseract: 这是一个Python库,它是Tesseract OCR的Python封装。 简单来说,它让我们可以用Python代码来调用Tesseract OCR的功能。

1. 安装 Tesseract OCR

安装Tesseract OCR的方法取决于你的操作系统:

  • Windows:

    1. 访问https://digi.bib.uni-mannheim.de/tesseract/ 下载最新版本的Tesseract OCR安装包(选择tesseract-ocr-w64-setup-v5.x.x.exe)。
    2. 运行安装包,按照提示完成安装。 注意: 记住你的Tesseract OCR安装路径,比如C:\Program Files\Tesseract-OCR,后面会用到。
  • macOS:

    可以使用Homebrew安装:

    brew install tesseract
    
  • Linux (Ubuntu):

    sudo apt update
    sudo apt install tesseract-ocr
    

2. 安装 pytesseract

安装好Tesseract OCR之后,就可以用pip安装pytesseract了:

pip install pytesseract

3. 配置 Tesseract OCR 路径 (重要!)

安装完 pytesseract 之后,需要告诉 pytesseract Tesseract OCR 的安装路径。 否则,pytesseract 找不到 Tesseract OCR,程序就没法正常工作。

import pytesseract

#  根据你的实际安装路径修改
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

注意:

  • C:\Program Files\Tesseract-OCR\tesseract.exe替换成你实际的Tesseract OCR安装路径。
  • 路径字符串前面加r是为了防止转义字符。

实战演练:从图片到文字

一切准备就绪,现在我们来写一个简单的Python程序,实现图片文字识别并保存到txt文件:

from PIL import Image
import pytesseract

# 配置 Tesseract OCR 路径 (如果之前没配置过)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# 1. 打开图片
image_path = 'test.png' # 替换成你的图片路径
img = Image.open(image_path)

# 2. 使用 pytesseract 识别图片中的文字
text = pytesseract.image_to_string(img, lang='chi_sim') #  lang='chi_sim' 指定识别中文

# 3. 打印识别结果
print(text)

# 4. 保存到 txt 文件
output_path = 'output.txt'
with open(output_path, 'w', encoding='utf-8') as f:
    f.write(text)

print(f'文字已保存到 {output_path}')

代码解释:

  1. 导入必要的库: PIL (Pillow) 用于打开图片, pytesseract 用于调用Tesseract OCR。
  2. 配置Tesseract OCR路径: 确保 pytesseract 知道 Tesseract OCR 的安装位置。
  3. 打开图片: 使用 Image.open() 函数打开你要识别的图片。 把 'test.png' 替换成你自己的图片路径。
  4. 识别图片中的文字: pytesseract.image_to_string() 函数是核心。 它接收一个Image对象作为输入,返回识别出的文字。 lang='chi_sim' 参数告诉 Tesseract OCR 识别中文。 如果你要识别英文,可以省略这个参数或者设置为 lang='eng'
  5. 打印识别结果: 把识别出的文字打印到控制台,方便查看。
  6. 保存到txt文件: 使用Python的文件操作,把识别出的文字保存到output.txt文件中。 encoding='utf-8' 参数指定使用UTF-8编码,以支持中文。

运行代码:

  1. 确保你已经正确安装了 Tesseract OCR 和 pytesseract,并且配置了 Tesseract OCR 的路径。
  2. 把上面的代码保存到一个.py文件中,比如ocr_example.py
  3. 准备一张包含文字的图片,比如test.png,放到和ocr_example.py同一个目录下。
  4. 在命令行中运行python ocr_example.py
  5. 如果一切顺利,你会在控制台看到识别出的文字,并且在当前目录下生成一个output.txt文件,里面保存了识别出的文字。

常见问题和解决方案

在使用Tesseract OCR的过程中,可能会遇到一些问题,这里列出一些常见的:

  • 识别精度不高:
    • 图片质量: Tesseract OCR 对图片质量要求比较高。 确保图片清晰,对比度足够。 可以尝试对图片进行预处理,比如二值化、降噪等。
    • 语言设置: 确保 lang 参数设置正确。 如果图片中包含多种语言,可以尝试指定多种语言,比如 lang='chi_sim+eng'
    • Tesseract OCR 版本: 尝试使用最新版本的 Tesseract OCR,新版本通常会改进识别精度。
  • 识别不出中文:
    • 安装中文语言包: 确保你安装了 Tesseract OCR 的中文语言包。 在Windows上,可以在安装 Tesseract OCR 时选择安装中文语言包。 在Linux上,可以使用 sudo apt install tesseract-ocr-chi-sim 安装。
    • lang 参数: 确保 lang 参数设置为 lang='chi_sim'
  • 报错:TesseractNotFoundError:
    • Tesseract OCR 路径: 这是最常见的问题。 确保你正确配置了 Tesseract OCR 的路径。

进阶技巧

  • 图片预处理: 对图片进行预处理可以显著提高识别精度。 常用的预处理方法包括:
    • 灰度化: 将彩色图片转换为灰度图片。
    • 二值化: 将灰度图片转换为黑白图片。
    • 降噪: 去除图片中的噪点。
    • 倾斜校正: 校正图片中的倾斜。
  • 自定义配置: Tesseract OCR 提供了丰富的配置选项,可以根据具体情况进行调整。 可以参考 Tesseract OCR 的官方文档。

总结

通过这篇文章,你应该已经掌握了使用Python和Tesseract OCR进行图片文字识别的基本方法。 希望你能用它来解决实际问题,或者开发出更有趣的应用! 快去尝试一下吧!

点评评价

captcha
健康