用途:从图片中提取文字,基于 Tesseract OCR 引擎,适合把截图、扫描件、照片中的文本转为可编辑内容。
主要能力:支持 PNG、JPG、JPEG、GIF、BMP、TIFF、WEBP 等格式;支持英语、简体中文、繁体中文、日语、韩语、法语、德语、西班牙语、俄语、阿拉伯语等 100 多种语言,可用 eng+chi_sim 形式指定多语言;可选 --preprocess 进行灰度、阈值化等预处理以提升准确率;输出纯文本或含置信度分数的 JSON。
使用方式: 基本用法:python3 scripts/ocr.py <image_file> <output_file> 指定语言(默认 eng):python3 scripts/ocr.py image.png text.txt --lang eng 中文:python3 scripts/ocr.py image.png text.txt --lang chi_sim 多语言:python3 scripts/ocr.py image.png text.txt --lang eng+chi_sim 预处理:python3 scripts/ocr.py image.png text.txt --preprocess JSON 输出:python3 scripts/ocr.py image.png output.json --format json 远程图片:python3 scripts/ocr_url.py <image_url> <output_file>,如 python3 scripts/ocr_url.py https://example.com/image.jpg text.txt --lang eng --preprocess 参数:image_file/image_url、output_file 必填;--lang 指定语言码;--preprocess 开启预处理;--format 选 text 或 json。
依赖与安装:Python 3.8+、pytesseract、Pillow,以及系统包 tesseract-ocr。安装命令:pip install pytesseract Pillow;Ubuntu/Debian 用 sudo apt-get install tesseract-ocr;CentOS/RHEL 用 sudo yum install tesseract;macOS 用 brew install tesseract。
适用场景:批量图片文字录入、截图信息提取、扫描文档数字化等。
限制:资料未提供识别准确率、处理速度、并发能力、收费与授权信息;实际效果取决于图片质量与所选语言包,非拉丁语种通常需额外安装对应语言数据。