将图片(含文字、公式、标题)转换为指定代码格式。自动识别标题级别(title1/title2/title3),文字行转为 $word->body(\"正文=\".$F);,公式转为 $word->formula(\"\");,图片标记为 ![image]
将包含文字、公式、图片的文档截图转换为指定的代码格式,支持 OCR 文字识别、公式识别和格式转换。
| 内容类型 | 格式模板 | 示例输入 | 示例输出 |
|---|---|---|---|
| 一级标题 | $word->title1(标题文字); | 第一章 项目概述 | $word->title1(项目概述); |
| 二级标题 |
| 级别 | 识别模式 | 提取规则 | 示例 |
|---|---|---|---|
| 一级标题 | 第 X 章 、 第 X 部分、 一、 | 去掉编号前缀 | 第一章 总述 → 总述 |
| 二级标题 |
处理逻辑:
python
def processtextline(text):
# 清理 OCR 结果
text = text.strip()
# 转义特殊字符
text = text.replace(, \\)
# 生成代码
return f$word->body(正文={text}=.$F);
识别流程:
判断规则:
输入:这是一段测试文字
输出:$word->body(正文=这是一段测试文字=.$F);
输入:E = mc²
输出:$word->formula(E = mc^2);
输入:∑(i=1 to n) xi
输出:$word->formula(\sum{i=1}^{n} xi);
输入:[图表图像]
输出:![image]
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(useanglecls=True, lang=ocr_lang)
def detectcontenttype(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 formulatolatex(self, formula_image: np.ndarray) -> str:
公式图像转 LaTeX
# 使用 pix2tex 或视觉 AI
pass
def convertline(self, linetext: 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.formulatolatex(formula_image)
return f$word->formula({latex});
elif content_type == image:
return ![image]
return
def processimage(self, imagepath: 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]
xmin, xmax = min(xcoords), max(xcoords)
ymin, ymax = min(ycoords), max(ycoords)
region = image[ymin:ymax, xmin:xmax]
# 检测内容类型
contenttype = self.detectcontent_type(region)
# 转换为代码格式
codeline = self.convertline(text, content_type, region)
outputlines.append(codeline)
# 输出结果
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 imagetocode.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.processimage(imagepath, output_path)
if not output_path:
print(result)
if name == main:
main()
输入图片内容:
第一章 项目概述
1.1 项目背景
本项目旨在开发一个智能系统
用于自动化文档处理
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 image-to-code-1776066371 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 image-to-code-1776066371 技能
skillhub install image-to-code-1776066371
文件大小: 39.99 KB | 发布时间: 2026-4-15 13:03