图片转 SVG 并生成 G 代码
下载地址
https://www.zhengdianxue.com/wp-content/uploads/skills/user_baad803c--img-to-gcode.zip将提示词发送给你的 AI 安装该 skills
请根据 https://skillhub.cn/install/skillhub.md,安装 @user_baad803c/img-to-gcode。复制图片路径,提交,然后你会得到: 1、一个svg失量图。 2、一个gcode代码(用于写字机绘图)。
阅读全文 (展开查看完整说明)
概述
img2gcode – Image to SVG to G-code Pipeline
Convert raster images to pen-plotter G-code via SVG vectorization. Outputs .svg and .gcode in the current working directory alongside the input image.
Workflow Overview
用户拖入图片 → save_image.py (保存到本地) → run.py (转 SVG + G-code)Step 1: 用户上传图片
用户在 WorkBuddy 中拖入一张图片,引用格式为 @image:<文件名> 或 @文件:<文件名>。
Step 2: 保存图片到临时文件夹
通过 save_image.py 将图片复制到当前工作目录,输出格式为 _input_<hash12>.<ext>:
python "{SKILL_DIR}/scripts/save_image.py" "<图片路径>"脚本会返回保存后的文件路径(例如 _input_a1b2c3d4e5f6.png),供下一步使用。
Step 3: 运行转换管线
将上一步输出的路径作为参数传入 run.py:
python "{SKILL_DIR}/scripts/run.py" "_input_a1b2c3d4e5f6.png"输出文件:
_input_a1b2c3d4e5f6.svg_input_a1b2c3d4e5f6.gcode
Step 4: 自动打开文件夹
run.py 运行完毕后会自动打开输出文件夹(调用 Windows Explorer),用户可直接查看 .svg 和 .gcode 文件。
Step 5: 在线查看 G-code
将生成的 .gcode 文件内容粘贴到 NCViewer 即可在线预览路径轨迹。
完整命令行示例(两步连用)
python "{SKILL_DIR}/scripts/save_image.py" "C:/Users/xxx/Desktop/photo.jpg"
python "{SKILL_DIR}/scripts/run.py" "_input_a1b2c3d4e5f6.jpg"或者图片已在磁盘上,直接运行:
python "{SKILL_DIR}/scripts/run.py" "C:/Users/xxx/Desktop/photo.jpg"Scripts
scripts/run.py
Main entry point. Accepts an image path, runs the full pipeline, outputs .svg and .gcode in the same directory.
scripts/img2svg.py
Image preprocessing and potrace invocation:
- Gaussian blur + CLAHE contrast enhancement
- Adaptive binary thresholding
- Morphological close/open to clean noise
- potrace with tuned parameters for smooth curve output
scripts/svg2gcode.py
SVG path to G-code conversion. Configurable pen-plotter parameters:
- Scale, offset, Y-flip, pen Z heights, feed rates, curve sampling
Dependencies
- Python packages:
opencv-python,numpy,svgpathtools - potrace 1.16 (bundled in
potrace-1.16.win64/potrace.exe)- 下载地址: https://potrace.sourceforge.net/download/1.16/potrace-1.16.win64.zip
- 安装方式: 下载 zip 并解压到本 SKILL 目录下,确保
potrace-1.16.win64/potrace.exe存在
Pen Plotter Parameters
Default values (edit in scripts/svg2gcode.py):
| Parameter | Default | Description |
|---|---|---|
| SCALE | 0.0018 | pixel → mm scale factor |
| OFFSET_X | 10.0 | X origin offset (mm) |
| OFFSET_Y | 20.0 | Y origin offset (mm) |
| PEN_UP_Z | 3.0 | Pen lift height (mm) |
| PEN_DOWN_Z | -0.5 | Pen drawing depth (mm) |
| FEED_MOVE | 3000 | Travel speed (mm/min) |
| FEED_DRAW | 1500 | Drawing speed (mm/min) |
| SAMPLE_COUNT | 18 | Bezier sampling points per curve |