← ClaudeAtlas

image-ocr-to-docslisted

批量 OCR 图片转可检索文档:PaddleOCR + 放大预处理(游戏截图/攻略图),输出同名 .md 或汇总清单,含双引擎交叉验证
Linearl/reasonix_skill_repo · ★ 2 · AI & Automation · score 63
Install: claude install-skill Linearl/reasonix_skill_repo
# 图片批量 OCR 转文档(Image OCR to Docs) 把图片目录批量 OCR 成可检索文字(生成同名 .md 或汇总清单),用于攻略截图、聊天记录、票据等图文资料的文字化。流程源自 个人工作目录 的 ocr_all_images.py,并集成了游戏截图(GBA 240x160 小字)的放大预处理经验。 ## 适用场景 - 图文攻略里的游戏截图/教学截图 → 文字化,让 grep 可检索 - 网页抓取下来的图片 → 提取文字 - 任何"一堆图片要转文字"的批处理 ## 环境依赖 - `paddleocr`(推荐,3.x)+ `paddlepaddle`;备选 `rapidocr-onnxruntime`(旧版 API 返回 `(result, elapse)` 元组,需适配) - `Pillow`(放大预处理) ## 执行流程 1. **确认目标目录**:列出图片清单(`ls`),确认扩展名和数量,`--dry-run` 预览 2. **写脚本执行**(核心逻辑): ```python # -*- coding: utf-8 -*- import io, sys, os sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') # Windows 必须,防 GBK 乱码 from PIL import Image, ImageOps, ImageEnhance from paddleocr import PaddleOCR IMG_DIR = "图片目录" ocr = PaddleOCR(lang="ch") def ocr_one(path): img = Image.open(path).convert("L") img = ImageOps.autocontrast(img) img = img.resize((img.width*4, img.height*4), Image.LANCZOS) # 小字/截图放大 4x img = ImageEnhance.Contrast(img).enhance(1.3) tmp = path + "_x4.png" img.save(tmp) res = ocr.predict(tmp) os.remove(tmp) texts = [] for line in res: if isinstance(line, dict) and "rec_texts" in line: for i, t in enumerate(line["rec_texts"]): conf = line["rec_scores"][i] if i < len(line["rec_scores"]) else 0 if conf >= 0.45: texts.append(t) return texts ``` 3. **输出两份产物**: - 每张图生成同名 `.md`(`# 文件名` + `> OCR自动识别` + 代码块包住文本)——逐图同名 md 模式 - 或合并成 `ocr_results.txt`(`===== 文件名 =====` 分���)——适合人工复核 4