图片OCR光学识别生成TXT文件
是什么:从图片中识别并提取文字内容。
为什么:将图片中的文字转换为可编辑的文本格式。
Python 示例
python
import pytesseract
from PIL import Image
def image_ocr(image_path):
"""图片OCR文字识别"""
# 打开图片
image = Image.open(image_path)
# OCR识别
text = pytesseract.image_to_string(image, lang='chi_sim+eng')
# 输出结果
print("识别结果:")
print(text)
return text
# 使用示例
text = image_ocr("text_image.jpg")
# 保存到文件
with open("output.txt", "w", encoding="utf-8") as f:
f.write(text)
安装依赖:
bash
pip install pytesseract pillow # 还需安装 Tesseract-OCR:https://github.com/UB-Mannheim/tesseract/wiki
语言包安装:
bash
# 中文识别需要下载语言包 # 下载 chi_sim.traineddata 放到 tessdata 目录
一句话总结:使用Tesseract OCR引擎从图片中提取文字,支持多语言识别,适合文档数字化和文字提取。