/media-processing:image-to-svgfrom media-processing
Converts raster images (photos, paintings, illustrations, line art) into SVG vector reproductions using data-driven color quantization and contour extraction. Handles graphic/line-art inputs via a compositional pipeline that extracts lines as SVG strokes.
Ships in media-processing. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Convert raster images into faithful SVG reproductions using data-driven color quantization and contour extraction. Never hand-draw shapes from visual interpretation — always extract geometry from the actual pixel data.
Trust the data, not your imagination. Claude's visual interpretation of images is unreliable for precise color matching, shape positioning, and spatial relationships. Every shape, color, and position must come from computational analysis of the source pixels.
pip install opencv-python-headless scikit-image scipy scikit-learn --break-system-packages -q
apt-get install -y librsvg2-bin -qq
import sys
sys.path.insert(0, '/mnt/skills/user/image-to-svg/scripts')
from pipeline import image_to_svg
svg, flow = image_to_svg("source.jpg", mode="painting")
with open("output.svg", "w") as f:
f.write(svg)
flow.summary() # timing + status per step
Look at the image and ask: "Does this have smooth gradients or hard edges?" Gradients → higher K. Hard edges → lower K.
| Mode | K | Best for | Dark shape gating |
|---|---|---|---|
"graphic" | 28 | Logos, icons, Kandinsky, flat design | Loose (keeps thin lines) |
"illustration" | 40 | Comics, editorial, digital art | Moderate |
"painting" | 56 | Renaissance, Impressionist, watercolor | Standard |
"photo" | 64 | Portraits, landscapes, still life | Standard (prevents woodcut artifacts) |
Default is "painting". When uncertain, start there.
Tradeoffs: K=64 produces ~2300 shapes (~1.2MB SVG) vs K=28's ~1000 shapes (~550KB), measured before v1.9.0's compact output, which is about 2.6x smaller (see Output Format). Processing time roughly doubles with K. The quality gain in tonal gradation is substantial for photos but wasted on graphic art.
All mode defaults (K, dark_lum, compactness_min, etc.) can be overridden via **kwargs:
svg, flow = image_to_svg("source.jpg", mode="graphic", K=12, min_area=20)
For images dominated by lines, strokes, and geometric shapes (Kandinsky, architectural drawings, technical illustrations, comic ink work), the standard fill-only pipeline produces jagged filled polygons instead of clean strokes. The compositional pipeline solves this with two passes:
Pass 1 — Line Extraction: Isolate thin features via morphological erosion → skeletonize to 1px centerlines → Hough line detection → merge collinear fragments → measure stroke width → sample color. Emits SVG <line> elements with stroke-width.
Pass 2 — Fill Extraction: Suppress line regions from image (replace with local background estimate via median blur) → run standard K-means quantization on the cleaned image → contour extraction → <path> fills.
Composition: Fills render behind strokes in layered <g> groups.
# Auto-detect: classifies input and routes automatically
svg, flow = image_to_svg("kandinsky.jpg", mode="graphic")
# Force compositional pipeline
svg, flow = image_to_svg("technical_drawing.png", mode="graphic", pipeline="compositional")
# Force fill-only (previous default behavior)
svg, flow = image_to_svg("photo.jpg", mode="painting", pipeline="fill")
Pipeline selection (pipeline parameter):
| Value | Behavior |
|---|---|
"auto" (default) | Classify input via edge density + luminance bimodality + Hough line count. Route to compositional for graphic art, fill-only for photos. |
"fill" | Force fill-only pipeline. Use for photos, paintings, or when compositional produces unwanted results. |
"compositional" | Force two-pass pipeline. Use for line art, technical drawings, or ink work where you know lines are present. |
Auto-classification heuristics: An image is classified as graphic when it has high edge density (>5% edge pixels) combined with bimodal luminance distribution (>0.35 bimodality coefficient), or high straight-line density (>3 Hough lines per 10k pixels).
SVG output structure (compositional):
<svg ...>
<rect ... /> <!-- background -->
<g id="fills" stroke-width="1" stroke-linejoin="round"> <!-- filled regions (painter's algorithm) -->
<path class="c3" d="m12 40l5-2..z"/>
</g>
<g id="strokes"> <!-- line strokes (on top) -->
<line x1="..." y1="..." x2="..." y2="..." stroke="#000" stroke-width="2.5" stroke-linecap="round"/>
</g>
</svg>
Stroke width control: Measured perpendicular to each detected line, then scaled by 0.65x and capped at 4.5 SVG units. This prevents thick features from rendering as bloated strokes while keeping thin lines crisp.
Current limitation — straight lines only: Hough transform detects straight segments. Curved strokes (arcs, spirals) are not yet extracted as strokes — they fall through to the fill pass. Future work: cv2.fitEllipse or spline fitting on skeleton branches.
Separate structure from color: K-means finds regions, palette remapping assigns bold colors. This produces screen-print / pop art effects.
# Named preset
svg, flow = image_to_svg("photo.jpg", mode="graphic", K=4, palette="pop")
# Custom hex list (darkest → lightest mapping order)
svg, flow = image_to_svg("photo.jpg", mode="graphic", K=8,
palette=["#000", "#dc143c", "#ff69b4", "#ffd700", "#32cd32", "#00bfff", "#ff8c00", "#f5f5f5"])
# Override background separately
svg, flow = image_to_svg("photo.jpg", mode="graphic", K=4, palette="ocean", bg_color="#000000")
Built-in presets: bw, mono3, mono4, pop, pop2, neon, warhol4, warhol6, warhol8, sunset, ocean
How it works: Unique shape colors are sorted by luminance. Palette entries are mapped proportionally — palette[0] replaces the darkest cluster, palette[-1] replaces the lightest. Background defaults to the lightest palette entry unless bg_color is set. Palette length doesn't need to match K exactly; colors are binned proportionally.
Portraits: Use K=16-24 even with bold palettes. Facial features (glasses, beard, brow) need tonal range that low K eliminates. A good rule of thumb: palette length ≈ K/3 for clean luminance binning. At K=8 with a 4-color palette, a face becomes an undifferentiated blob.
Contrast preprocessing warning: External contrast boosting (contrast-stretch, sigmoidal-contrast) can confuse background detection. The pipeline's edge-contact heuristic assumes untouched luminance distributions — aggressive tone-mapping pushes subject tones into background-adjacent bins, causing misclassification (e.g., dark jacket regions classified as background and mapped to the lightest palette color). If you see subject regions tearing to the background color, try without preprocessing first. The pipeline's own bilateral blur + optional kuwahara/oilpaint handles tonal separation.
bg_clusters)Control which clusters are treated as background:
# Auto-detect (default) — edge-contact heuristic
svg, flow = image_to_svg("photo.jpg", mode="illustration", K=20, palette="warhol6")
# Disable — no clusters removed, no background rect color override
svg, flow = image_to_svg("photo.jpg", mode="illustration", K=20, palette="warhol6", bg_clusters=0)
# Force specific cluster indices (from quantize step's sorted_clusters output)
svg, flow = image_to_svg("photo.jpg", mode="illustration", K=20, palette="warhol6", bg_clusters=[2, 5])
Use bg_clusters=0 when palette remapping already controls all colors explicitly and background detection is getting in the way. Use bg_clusters=[list] when you know which clusters are background but the heuristic misidentifies them.
# Key: enough K for facial features, palette length ~K/3, modest smoothing
# Do NOT apply contrast preprocessing — it breaks background detection.
results = image_to_svg_batch("portrait.jpg", [
{"name": "hot", "mode": "illustration", "K": 20, "smooth": "kuwahara:6",
"palette": ["#000", "#D4145A", "#FF6B9D", "#FF85C0", "#FFD700", "#FFEF82", "#FFF8DC"]},
{"name": "cool", "mode": "illustration", "K": 20, "smooth": "kuwahara:6",
"palette": ["#0D0035", "#4A00E0", "#7B68EE", "#00D4FF", "#7FFFD4", "#B0FFE0", "#E0FFFF"]},
{"name": "earth", "mode": "illustration", "K": 20, "smooth": "kuwahara:6",
"palette": ["#1a0a00", "#8B4513", "#CD853F", "#DEB887", "#F5DEB3", "#FAEBD7", "#FFF8DC"]},
{"name": "neon", "mode": "illustration", "K": 20, "smooth": "kuwahara:6",
"palette": ["#0d0d0d", "#ff00ff", "#00ff00", "#ffff00", "#00ffff", "#ff69b4", "#f5f5f5"]},
], svg_width=700)
Why this works: K=20 preserves enough tonal clusters for facial structure (glasses, beard, brow ridge). 7-color palettes give ~K/3 luminance bins — enough variation to separate features without muddying. kuwahara:6 smooths texture without dissolving edges (:12 erases glasses). Raw source → pipeline smoothing only; no external contrast manipulation.
Reduce shape count and SVG file size by 20-45% using ImageMagick edge-preserving filters before quantization. Requires ImageMagick on PATH (pre-installed on Claude.ai containers).
# Oilpaint: bold, painterly smoothing (default strength=8)
svg, flow = image_to_svg("photo.jpg", mode="photo", smooth="oilpaint")
# Stronger smoothing = fewer shapes, more stylized
svg, flow = image_to_svg("photo.jpg", mode="illustration", K=32, smooth="oilpaint:12")
# Kuwahara: subtler, preserves more structure (default strength=5)
svg, flow = image_to_svg("photo.jpg", mode="painting", smooth="kuwahara:7")
# Works with batch API too
results = image_to_svg_batch("photo.jpg", [
{"name": "raw", "mode": "photo"},
{"name": "smooth", "mode": "photo", "smooth": "oilpaint"},
{"name": "stylized", "mode": "illustration", "K": 32, "smooth": "oilpaint:12", "palette": "pop"},
])
Available filters: oilpaint (ImageMagick -paint), kuwahara (ImageMagick -kuwahara). Append :N for custom strength.
How it works: The IM filter runs before the pipeline's bilateral+Gaussian blur. Both are edge-preserving smoothers at different scales — IM handles coarse texture, bilateral handles fine detail. The result is cleaner K-means regions with fewer fragmented shapes.
Measured impact (1206×1597 photo, K=32):
| smooth | Shapes | SVG size | Reduction |
|---|---|---|---|
| none | 3381 | 1868KB | — |
| oilpaint (8) | 2385 | 1329KB | -29% |
| oilpaint:12 | 1842 | 1065KB | -43% |
| kuwahara (5) | 2719 | 1453KB | -22% |
| kuwahara:7 | 2000 | 1152KB | -38% |
Uses the flowing DAG runner. Steps with independent inputs run in parallel.
pipeline="fill")preprocess → quantize → ┬─ detect_background ─┬─ extract_contours → assemble_svg
└─ edge_map ─┘
pipeline="compositional")classify_input ──→ extract_lines ──→ suppress_line_regions ──→ [fill pipeline on cleaned image]
│ │
└──────────── lines ───────────────────→ assemble_compositional ←── fills
Steps (fill-only):
cv2.Sobel (parallel with detect_background)Additional steps (compositional):
svg, flow = image_to_svg("source.jpg", mode="photo")
# If extract_contours failed:
flow.override(extract_contours, corrected_shapes)
flow.resume() # quantize, detect_background, edge_map stay cached
Generate multiple variants from one image, sharing computation across runs with the same K:
from pipeline import image_to_svg_batch
results = image_to_svg_batch("photo.jpg", [
{"name": "photo", "mode": "photo"},
{"name": "warhol", "mode": "graphic", "K": 12, "palette": "warhol4"},
{"name": "neon", "mode": "graphic", "K": 12, "palette": "neon"},
{"name": "sunset", "mode": "graphic", "K": 12, "palette": "sunset"},
{"name": "bw", "mode": "graphic", "K": 16, "palette": "bw"},
], svg_width=1400)
for name, svg in results.items():
with open(f"{name}.svg", "w") as f:
f.write(svg)
Variants sharing the same K run the pipeline (preprocess → quantize → edge_map → extract_contours) once, then fan out at assembly for palette remapping. This guarantees structural identity across palette variants (same shapes, same paths) and saves ~20-60s per shared K group.
Verification still applies in batch mode. The turnkey feel of batch processing makes it easy to skip the side-by-side comparison — don't. Render at least one variant per K group and verify before delivering. Background detection failures and palette mapping issues are invisible without rendering.
After EVERY run, render and visually compare side-by-side. This is non-negotiable.
import subprocess
from PIL import Image
subprocess.run(['rsvg-convert', '-w', '1400', 'output.svg', '-o', 'output.png'])
orig = Image.open('source.jpg')
rendered = Image.open('output.png')
target_h = 800
orig_r = orig.resize((int(orig.width * target_h / orig.height), target_h))
rend_r = rendered.resize((int(rendered.width * target_h / rendered.height), target_h))
gap = 20
comp = Image.new('RGB', (orig_r.width + rend_r.width + gap, target_h), (255,255,255))
comp.paste(orig_r, (0, 0))
comp.paste(rend_r, (orig_r.width + gap, 0))
comp.save('comparison.png')
# LOOK AT comparison.png BEFORE claiming success
When two regions have similar luminance but different hue/saturation, K-means in RGB space merges them. Use HSV multispectral analysis:
hsv = cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV)
h_ch, s_ch, v_ch = hsv[:,:,0], hsv[:,:,1], hsv[:,:,2]
# Separate gray (low saturation) from red (high saturation) at similar brightness
red_mask = ((h_ch < 12) | (h_ch > 168)) & (s_ch > 120) & (v_ch > 80)
gray_mask = (s_ch < 80) & (v_ch > 40) & (v_ch < 120) & spatial_constraint
Saturation is the key discriminator for colors that look similar in grayscale but are visually distinct.
When adding shapes not captured by quantization, derive coordinates from the SVG render, not the source image. The extraction pipeline shifts positions due to contour simplification.
# WRONG: extract from source, insert into SVG (coordinate mismatch)
# RIGHT: render SVG → detect gap in render → create shape in render coords → insert
svg_render = cv2.imread('rendered_svg.png')
Since v1.9.0 fills are written compactly. On Starry Night the painting output went from 3.64 MB to 1.35 MB (825 KB → 293 KB gzipped), rendering identically in Chromium and librsvg:
<svg xmlns="http://www.w3.org/2000/svg" id="s1a2b3c" viewBox="0 0 1000 625">
<style>#s1a2b3c .c0{fill:#3e85bb;stroke:#3e85bb}#s1a2b3c .c1{...}</style>
<rect width="1000" height="625" fill="#16151e"/>
<g stroke-width="1" stroke-linejoin="round">
<path class="c0" d="m793 0l-10.8 2.9-1 14.7..z"/>
</g>
</svg>
<path> (draw order kept; evenodd shapes stay separate), so the element count is below the shape count.m/l commands on a grid of 10^-precision units. precision=1 (default) matches the contour grid and is lossless; precision=0 rounds to whole viewBox units for about 20% less, at a mean pixel difference of ~1%."M x,y L x,y Z" path strings internally (flow.value(extract_contours)["shapes"]); only the emitted SVG is compact.@keyframes k{50%{fill:#f0f;stroke:#f0f}} #s1a2b3c .c0{animation:k 4s infinite}.Every fill <path> is stroked with its own fill color, at stroke-width="{gap_stroke}" with round joins (set once on the fills group). This bleeds each shape outward with its own fill color, covering inter-cluster gaps with the locally correct color.
Auto-scaling: gap_stroke is computed as max(1.0, round(svg_width / source_width)). A 500px source at svg_width=1000 gets gap_stroke=2; a 1000px source gets gap_stroke=1. This prevents the "snow" artifact where high-shape-count SVGs show visible light halos at zoom-out from excessive stroke bleed. Override explicitly: image_to_svg("img.jpg", gap_stroke=3).
Why stroke beats dilation for gaps: Dilation operates on binary masks before contour simplification — it blurs detail. Stroke operates on final polygons after approxPolyDP — it catches all gaps including those introduced by simplification. Pure vector; since v1.9.0 the stroke costs only the stroke declaration in each color's class.
Background fallback: When detect_background finds no clusters, the bg rect uses #000000 (black) instead of white. Black reads as shadow; white reads as absence.
Dilation is reduced to iterations=1 — just enough for morphological noise cleanup. Gap coverage is fully handled by stroke.
iterations=1 on a 3×3 kernel — stroke=fill handles gap coverage in vector space, so dilation only needs to close noise holes."graphic" mode loosens this, but very fine crosshatching may still degrade. Use pipeline="compositional" for line-art inputs — it extracts thin features as SVG strokes instead.cv2.fitEllipse or spline fitting on skeleton branches.pip install opencv-python-headless scikit-image scipy scikit-learn --break-system-packages
apt-get install -y librsvg2-bin # for rsvg-convert
Compiled acceleration: nn_assign.c is auto-compiled on first use if gcc is available (27x faster label assignment). Falls back to numpy if unavailable.
Cross-skill dependencies (resolved automatically by pipeline.py):
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub oaustegard/claude-skills --plugin media-processing17 skills in this plugin
/animating-with-codeMake a short animated film by writing the drawing code yourself: every frame a pure function of time, rendered headlessly to MP4. Covers storyboarding with a per-shot timing sheet of viewer 'reads', blocking key poses as stills, the contact-sheet / frame-strip / crop review loop, and the hand-painted character kit JohnHeibel/ClaudeAnimationBase (p5.js + p5.brush, the Clawd mascot, 31 emotions). Use for 'animate', 'make a cartoon', 'animated short', 'motion graphic', 'Clawd video', 'p5 animation', or any video request where Claude draws the frames. Not for AI-generated footage (creating-video), transcoding or trimming existing video (processing-video), or a comic from footage (drawing-video)./converting-filesConvert a file from one format to another inside the container — documents, images, audio, video. Routes to the right engine (pandoc, LibreOffice, ImageMagick, ffmpeg) by format pair. Triggers on "convert X to Y", "turn this docx into a pdf", "make a gif from this mp4", "md to docx", "batch-convert these images", or any single-file or batch format change where the source and target extensions differ. NOT for editing content (use docx/pptx/xlsx/pdf skills), creating files from scratch, or reading a file you already have in context./creating-videoCreate video from prompts by overseeing multi-clip AI generation end to end: write a shot list, generate each scene with Gemini Omni Flash (via the Cloudflare AI Gateway), review the results, and assemble them into a finished cut. Use when the user asks to make/generate a video, a short film, an animatic, or a multi-scene clip from a script or idea; when they mention Omni, Veo, text-to-video, or image-to-video; or when acting as the editing/director agent over generated footage. Triggers on 'make a video', 'generate a clip', 'short film', 'video from this script', 'turn this into a video', 'omni', 'veo', 'text to video', 'storyboard to video'. For transcoding/trimming/merging/GIF/subtitles use processing-video; for reading or summarizing existing video content use parsing-video./detecting-tips-zonesText-prompted image zone detection using TIPSv2 B/14 on CPU. Produces `focus_targets` / `focus_edges` bbox lists from natural-language labels, ready to feed into `svg-portrait-mode`. Use when you want automatic foreground/background separation from prompts like "dog face" + "wooden floor" instead of hand-annotating bboxes./drawing-videoTurn a short video into a narrated comic strip. Use when the user uploads a video (.mov/.mp4/etc.) and asks to make a comic from it, narrate it, draw it, Attenborough it, storyboard the clip, or otherwise wants a stills-plus-narration comic strip derived from footage. Covers probing and extracting frames, GROUNDING the actual storyline (native Gemini video parse or frame reading), composing narration in a chosen voice, generating comic panels with Gemini image models anchored to the real stills, QA-ing the panels, and compositing a titled strip. Do NOT use for plain video transcoding or trimming (that is processing-video) or for original illustration from a text prompt (that is invoking-gemini)./drawing-with-pycairoDraws images, PDF/SVG vector files and animation frames with pycairo, and supplies what cairo lacks: shaped text for Arabic, Indic and other scripts, text along curves, blur/glow/drop shadows, path booleans, stroke-to-outline, and Gouraud-shaded 3D. Use when writing pycairo or cairo drawing code, generative art, custom diagrams or posters rendered in Python, or when cairo output needs non-Latin text, soft effects, merged shapes or shaded surfaces.Other plugins' skills for the same job