pix2svg

Crates.iopix2svg
lib.rspix2svg
version0.1.0
created_at2025-06-04 05:08:13.574325+00
updated_at2025-06-04 05:08:13.574325+00
descriptionConvert pixel art images to optimized SVG format
homepage
repositoryhttps://github.com/LeSnow-Ye/pix2svg
max_upload_size
id1699800
size343,389
LeSnow Ye (LeSnow-Ye)

documentation

README

pix2svg

Warning: This project is mainly created by GitHub Copilot with Claude Sonnet 4. Not fully reviewd and tested yet. Use it at your own risk. PRs and issues are welcome.

A fast and efficient command-line tool to convert pixel art images to optimized SVG format with greedy rectangle merging.

Installation

From Source

git clone https://github.com/LeSnow-Ye/pix2svg.git
cd pix2svg
cargo build --release

The binary will be available at target/release/pix2svg.

Using Cargo

cargo install pix2svg

Usage

Basic Usage

# Convert image.png to image.svg
pix2svg image.png

# Specify output file
pix2svg image.png -o output.svg

# Scale pixels by 4x
pix2svg image.png -s 4

# Enable verbose output
pix2svg image.png -v

Advanced Options

# Convert with custom alpha threshold
pix2svg image.png --alpha-threshold 128

# Scale up and specify output
pix2svg sprite.png -s 8 -o large-sprite.svg -v

Command Line Options

  • input: Input image file path
  • -o, --output: Output SVG file path (default: input filename with .svg extension)
  • -s, --scale: Pixel scale factor (1-1000, default: 1)
  • --alpha-threshold: Minimum alpha threshold for non-transparent pixels (0-255, default: 1)
  • --skip-transparent: Skip transparent pixels (default: true)
  • -f, --force: Force overwrite existing output files
  • -v, --verbose: Enable verbose output
  • -h, --help: Show help information

Algorithm

The tool uses a greedy rectangle merging algorithm similar to the one used in Aseprite:

  1. Scan pixels: Process image pixel by pixel from top-left to bottom-right
  2. Find rectangles: For each unprocessed pixel, find the largest rectangle of the same color
  3. Optimize: Greedily expand rectangles horizontally and vertically
  4. Output: Generate SVG <rect> elements for each rectangle

This approach significantly reduces the number of SVG elements compared to naive pixel-by-pixel conversion.

Examples

Input

Source: CC-29 Palette

464x120 = 55680 pixels

Output

4738 rectangles (8.51%)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 9

cargo fmt