img2svg
A high-performance image to SVG converter written in Rust. Transform raster images (PNG, JPEG, etc.) into scalable vector graphics with advanced algorithms for color quantization, contour tracing, and path optimization.
Why img2svg?
The Problem
Converting raster images to vector format is essential for:
- Scalability: Vector graphics scale infinitely without quality loss
- File size optimization: Simple shapes often result in smaller SVG files than raster counterparts
- Editability: Vectors can be modified in design tools (Illustrator, Inkscape, Figma)
- Web performance: SVGs are code-based and can be optimized, animated, and styled with CSS
Why img2svg?
| Feature | img2svg | ImageMagick | Potrace | Vector Magic |
|---|---|---|---|---|
| Pure Rust | ✅ | ❌ (C++) | ❌ (C) | ❌ (Web only) |
| Color Support | ✅ Full color | ✅ | ❌ B&W only | ✅ |
| Library API | ✅ | ❌ CLI only | ❌ CLI only | ❌ Web only |
| MCP Server | ✅ | ❌ | ❌ | ❌ |
| Local Processing | ✅ | ✅ | ✅ | ❌ (Cloud) |
| Open Source | ✅ | ✅ | ✅ | ❌ |
| Advanced Algorithms | ✅ | ⚠️ Basic | ⚠️ Basic | ✅ |
Key Advantages
- Smart Color Quantization: Median-cut algorithm preserves visual quality while reducing colors
- Sub-pixel Accuracy: Marching squares algorithm produces precise contours
- Path Optimization: RDP simplification creates compact SVG files without losing detail
- Flexible Usage: CLI tool, Rust library, and MCP server
- Zero Dependencies: No external image processing libraries required
Installation
CLI Tool
# From crates.io
# From source
Library
Add to your Cargo.toml:
[]
= "0.1"
MCP Server
Usage
CLI Tool
# Basic conversion
# High-quality conversion with more colors
# Simple logo with fewer colors
# Batch convert multiple files
for; do
done
Options
| Option | Short | Default | Description |
|---|---|---|---|
--input |
-i |
required | Input image file (PNG, JPEG, etc.) |
--output |
-o |
auto | Output SVG file (defaults to input with .svg extension) |
--colors |
-c |
16 | Number of colors for quantization (1-64) |
--threshold |
-t |
0.1 | Edge detection threshold (0.0-1.0) |
--smooth |
-s |
5 | Path smoothing level (0-10) |
--hierarchical |
false | Enable hierarchical decomposition | |
--advanced |
-a |
false | Use advanced SVG generation |
Rust Library
use ;
use Path;
// Simple conversion with defaults
let options = default;
convert?;
// Custom options for better quality
let options = ConversionOptions ;
convert?;
// Low-detail conversion for simple graphics
let options = ConversionOptions ;
MCP Server
The MCP (Model Context Protocol) server is built into the same codebase and allows AI assistants (like Claude Desktop) to convert images to SVG directly.
Installation
The MCP server binary is built automatically with the main project:
# Build both CLI and MCP server
# Or install both binaries
The binaries will be:
img2svg- CLI toolimg2svg-mcp- MCP server
Configuration for Claude Desktop
Add to your Claude Desktop MCP configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
Replace /path/to/img2svg-mcp with the full path to the installed binary:
- If installed via
cargo install: Runwhich img2svg-mcpto find the path - If built locally: Usually at
target/release/img2svg-mcp
Usage
Once configured, restart Claude Desktop and use the tool directly in conversations:
"Convert the image at /path/to/logo.png to SVG with 8 colors"
The MCP server provides one tool:
convert_image_to_svg: Converts raster images to SVG formatinput_path(required): Path to input imageoutput_path(required): Path for output SVGnum_colors(optional): Number of colors (1-64, default: 16)smooth_level(optional): Smoothing level (0-10, default: 5)threshold(optional): Edge detection threshold (0.0-1.0, default: 0.1)
Algorithm
img2svg uses a sophisticated multi-stage pipeline:
- Color Quantization: Median-cut algorithm reduces the palette while preserving color distribution
- Pixel Grouping: Groups pixels by quantized color, sorted by area for proper z-order
- Contour Tracing: Marching squares on per-color binary masks produces sub-pixel-accurate boundaries
- Path Smoothing: Gaussian neighbor averaging reduces jaggedness without adding points
- Path Simplification: Ramer-Douglas-Peucker algorithm reduces point count (epsilon=2.0)
- Edge Snapping: Points near image boundaries are snapped to exact edges
- SVG Generation: Background rect + one
<path>per color with mergedM...Zsubpaths
Examples & Quality
The examples/ directory contains sample conversions demonstrating quality:
Simple Graphics (8 colors)
Input: examples/input/simple.png (50x50, basic shapes)
Output: examples/output/simple.svg
- Command:
img2svg -i simple.png -o simple.svg -c 8 -s 3 - Result: Clean vector shapes with perfect edges
- File size: Often smaller than original PNG
Gradients (16 colors)
Input: examples/input/gradient.png (100x100, smooth gradient)
Output: examples/output/gradient.svg
- Command:
img2svg -i gradient.png -o gradient.svg -c 16 -s 5 - Result: Smooth color transitions with banding minimized
Complex Illustrations (32 colors)
Input: examples/input/complex.png (200x200, detailed)
Output: examples/output/complex.svg
- Command:
img2svg -i complex.png -o complex.svg -c 32 -s 7 - Result: Preserves fine details while reducing complexity
Comparison with Alternatives
# ImageMagick trace (often produces jagged edges)
# img2svg (smooth curves, better color accuracy)
Quality Differences:
- img2svg: Smooth curves, accurate colors, compact paths
- ImageMagick: Often produces jagged edges, limited color optimization
- Potrace: B&W only, requires pre-processing for color images
Performance
img2svg is optimized for speed and memory:
- Speed: Typical 1000x1000 image converts in <1 second
- Memory: Efficient streaming processing, suitable for large images
- Parallelization: Color quantization can be parallelized for large palettes
Benchmarks (1000x1000px image):
| Colors | Time | Output Size |
|---|---|---|
| 8 | 0.3s | 45 KB |
| 16 | 0.5s | 78 KB |
| 32 | 0.8s | 156 KB |
| 64 | 1.4s | 312 KB |
Tips for Best Results
For Logos and Icons
- Use fewer colors (8-16)
- Lower smoothing (2-4)
- Higher threshold (0.15-0.2)
For Photos
- More colors (32-64)
- Higher smoothing (6-10)
- Lower threshold (0.05-0.1)
For Illustrations
- Medium colors (16-32)
- Medium smoothing (4-6)
- Default threshold (0.1)
Limitations
- Best with images having clear color boundaries
- Photorealistic images may need higher color counts
- Very complex gradients may show some banding
- Extremely detailed images may produce large SVG files
Contributing
Contributions are welcome! Please see TODO.md for planned improvements.
License
Apache License 2.0 - see LICENSE for details.
Acknowledgments
- Median-cut algorithm: Paul Heckbert (1980)
- Marching squares: William E. Lorensen (1987)
- RDP algorithm: Ramer & Douglas & Peucker (1972-1973)