返回顶部
i

image-to-code图片转代码

将图片(含文字、公式、标题)转换为指定代码格式。自动识别标题级别(title1/title2/title3),文字行转为 $word->body(\"正文=\".$F);,公式转为 $word->formula(\"\");,图片标记为 ![image]

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 2.0.0
安全检测
已通过
176
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

image-to-code

图片转代码格式转换器

功能概述

将包含文字、公式、图片的文档截图转换为指定的代码格式,支持 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 架构 → 架构 |

执行流程

阶段一:图片预处理

  1. 1. 图像增强
- 灰度化处理 - 二值化(文字区域) - 去噪点
  1. 2. 区域分割
- 文字区域检测 - 公式区域检测 - 图片区域检测
  1. 3. 顺序识别
- 从上到下扫描 - 从左到右排序 - 保持原始顺序

阶段二:内容识别

2.1 文字识别 (OCR)

工具: PaddleOCR / Tesseract / 视觉 AI

处理逻辑:
python
def processtextline(text):
# 清理 OCR 结果
text = text.strip()
# 转义特殊字符
text = text.replace(, \\)
# 生成代码
return f$word->body(正文={text}=.$F);

2.2 公式识别

工具: Pix2Tex / MathOCR / 视觉 AI

识别流程:

  1. 1. 检测公式区域(特殊字体、符号)
  2. 转换为 LaTeX 格式
  3. 生成 formula 代码

判断规则:

  • - 包含数学符号:∑∫∂∇√∞≈≠≤≥±×÷
  • 包含变量:x, y, z, α, β, γ, θ
  • 包含上标/下标格式
  • 独立成行的数学表达式

2.3 图片识别

判断规则:
  • - 图表区域(坐标轴、图例)
  • 流程图/框图
  • 非文字非公式的图像内容

阶段三:格式转换

3.1 文字行处理

输入:这是一段测试文字
输出:$word->body(正文=这是一段测试文字=.$F);

3.2 公式处理

输入:E = mc²
输出:$word->formula(E = mc^2);

输入:∑(i=1 to n) xi
输出:$word->formula(\sum{i=1}^{n} xi);

3.3 图片处理

输入:[图表图像]
输出:![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(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.1 项目背景
本项目旨在开发一个智能系统
用于自动化文档处理

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 image-to-code-1776066371 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 image-to-code-1776066371 技能

通过命令行安装

skillhub install image-to-code-1776066371

下载

⬇ 下载 image-to-code v2.0.0(免费)

文件大小: 39.99 KB | 发布时间: 2026-4-15 13:03

v2.0.0 最新 2026-4-15 13:03
Manual publish - scheme 1

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部