将包含文字、公式、图片的文档截图转换为指定的代码格式,支持 OCR 文字识别、公式识别和格式转换。
内容类型格式模板示例输入示例输出一级标题$word->title1("标题文字");``第一章 项目概述``$word->title1("项目概述");二级标题$word->title2("标题文字");``1.1 项目背景
(1) 提高效率``$word->title2("项目背景");
$word->title2("提高效率");三级标题$word->title3("标题文字");``1.1.1 技术路线``$word->title3("技术路线");文字行$word->body("正文=内容=".$F);``这是正文``$word->body("正文=这是正文=".$F);公式$word->formula("LaTeX 公式");``E = mc²``$word->formula("E = mc^2");图片![image][图表]![image]空行保持空行(空)(空行)
级别识别模式提取规则示例一级标题第 X 章 、 第 X 部分、 一、去掉编号前缀第一章 总述 → 总述二级标题第 X 节、 1.1、 (1)、 (一)去掉编号前缀1.1 背景 → 背景
(1) 提高 → 提高三级标题1.1.1、 1、去掉编号前缀1.1.1 架构 → 架构
工具: PaddleOCR / Tesseract / 视觉 AI
处理逻辑:
python
def process_text_line(text):
# 清理 OCR 结果
text = text.strip()
# 转义特殊字符
text = text.replace('"', '\\"')
# 生成代码
return f'$word->body("正文={text}=".$F);'
工具: Pix2Tex / MathOCR / 视觉 AI
识别流程:
判断规则:
判断规则:
text
输入:这是一段测试文字
输出:$word->body("正文=这是一段测试文字=".$F);
text
输入:E = mc²
输出:$word->formula("E = mc^2");
输入:∑(i=1 to n) xi
输出:$word->formula("\sum_{i=1}^{n} x_i");
text
输入:[图表图像]
输出:![image]
python
# OCR
paddlepaddle
paddleocr
# 公式识别
pix2tex
latex2sympy
# 图像处理
opencv-python
Pillow
numpy
# 可选:视觉 AI
openai # GPT-4V
anthropic # Claude Vision
python
#!/usr/bin/env python3
"""
图片转代码格式转换器
将图片中的文字、公式、图表转换为指定代码格式
"""
import cv2
import numpy as np
from pathlib import Path
from paddleocr import PaddleOCR
from typing import List, Tuple, Dict
class ImageToCodeConverter:
def __init__(self, ocr_lang='ch'):
"""初始化 OCR 引擎"""
self.ocr = PaddleOCR(use_angle_cls=True, lang=ocr_lang)
def detect_content_type(self, image_region: np.ndarray) -> str:
"""
检测内容类型
返回:'text' | 'formula' | 'image'
"""
# 分析区域特征
# 公式:特殊符号密度高、字体变化大
# 图片:颜色丰富、边缘复杂
# 文字:规则排列、对比度高
pass
def ocr_text(self, image: np.ndarray) -> List[Dict]:
"""执行 OCR 识别"""
result = self.ocr.ocr(image, cls=True)
return result
def formula_to_latex(self, formula_image: np.ndarray) -> str:
"""公式图像转 LaTeX"""
# 使用 pix2tex 或视觉 AI
pass
def convert_line(self, line_text: str, content_type: str) -> str:
"""
转换单行内容为代码格式
"""
if content_type == 'text':
# 转义双引号
escaped = line_text.replace('"', '\\"')
return f'$word->body("正文={escaped}=".$F);'
elif content_type == 'formula':
latex = self.formula_to_latex(formula_image)
return f'$word->formula("{latex}");'
elif content_type == 'image':
return '![image]'
return ''
def process_image(self, image_path: str, output_path: str = None):
"""
处理整张图片
"""
# 读取图片
image = cv2.imread(image_path)
# OCR 识别
ocr_result = self.ocr.ocr(image, cls=True)
# 按行处理
output_lines = []
for line in ocr_result:
if line:
for text_box in line:
bbox = text_box[0]
text = text_box[1][0]
confidence = text_box[1][1]
# 提取区域图像
x_coords = [p[0] for p in bbox]
y_coords = [p[1] for p in bbox]
x_min, x_max = min(x_coords), max(x_coords)
y_min, y_max = min(y_coords), max(y_coords)
region = image[y_min:y_max, x_min:x_max]
# 检测内容类型
content_type = self.detect_content_type(region)
# 转换为代码格式
code_line = self.convert_line(text, content_type, region)
output_lines.append(code_line)
# 输出结果
output = '\n'.join(output_lines)
if output_path:
with open(output_path, 'w', encoding='utf-8') as f:
f.write(output)
return output
def main():
import sys
if len(sys.argv) < 2:
print("用法:python image_to_code.py <图片路径> [输出路径]")
sys.exit(1)
image_path = sys.argv[1]
output_path = sys.argv[2] if len(sys.argv) > 2 else None
converter = ImageToCodeConverter()
result = converter.process_image(image_path, output_path)
if not output_path:
print(result)
if __name__ == '__main__':
main()
输入图片内容:
text
第一章 项目概述
1.1 项目背景
本项目旨在开发一个智能系统
用于自动化文档处理
(1) 减少人工操作
(2) 提高准确性
输出代码:
php
$word->title1("项目概述");
$word->title2("项目背景");
$word->body("正文=本项目旨在开发一个智能系统=".$F);
$word->body("正文=用于自动化文档处理=".$F);
$word->title2("减少人工操作");
$word->title2("提高准确性");
输入图片内容:
text
第三章 物理公式
3.1 牛顿第二定律
F = ma
力的单位:牛顿 (N)
3.2 万有引力
F = G(m₁m₂)/r²
输出代码:
php
$word->title1("第三章 物理公式");
$word->title2("3.1 牛顿第二定律");
$word->formula("F = ma");
$word->body("正文=力的单位:牛顿 (N)=".$F);
$word->title2("3.2 万有引力");
$word->formula("F = G\frac{m_1 m_2}{r^2}");
输入图片内容:
text
销售数据对比
[柱状图]
结论:Q4 增长明显
输出代码:
php
$word->body("正文=销售数据对比=".$F);
![image]
$word->body("正文=结论:Q4 增长明显=".$F);
bash
# 基本用法
python image_to_code.py input.png
# 指定输出文件
python image_to_code.py input.png output.txt
# 批量处理
python image_to_code.py *.png --output-dir ./output
# 使用视觉 AI(更准确的公式识别)
python image_to_code.py input.png --use-vision-ai
json
{
"ocr_engine": "paddleocr",
"ocr_lang": "ch",
"formula_detection": "auto",
"formula_engine": "pix2tex",
"vision_ai": {
"enabled": false,
"provider": "openai",
"model": "gpt-4-vision-preview"
},
"output": {
"encoding": "utf-8",
"line_ending": "\n"
}
}
使用 GPT-4V/Claude Vision 提高公式识别准确率
根据前后文自动校正 OCR 错误
支持文件夹批量转换
测试类型输入预期输出纯中文"你好世界"$word->body("正文=你好世界=".$F);中英文混合"Hello 世界"$word->body("正文=Hello 世界=".$F);简单公式"a + b = c"$word->formula("a + b = c");复杂公式"∫₀^∞ e^(-x²)dx"$word->formula("\int_{0}^{\infty} e^{-x^2}dx");图片[图表]![image]空行(空)(空行)
图片转代码,让文档处理更高效 🐘