Image to SVG Color Mapping
← Project Index Image to SVG Color Mapping

Image to SVG Color Mapping

A pen-plotter tool that turns a raster image into multi-pen SVG -; seven drawing styles, K-means color reduction to match the pens you actually own, TSP path optimization to cut plot time, and a separate file per pen color.

Archived Started: Summer 2025 Updated: Summer 2025

Overview

A pen plotter draws with real pens, one color at a time, following vector paths -; so to plot a photo you have to translate continuous color into a finite set of pens and a set of strokes the machine can physically draw. This tool does that translation: it reduces an image to a handful of pen colors, renders each region in one of seven drawing styles, and exports a clean SVG per color plus a combined preview.

It runs as both a Tkinter GUI and a command-line tool -; same converter underneath, with the program auto-detecting which mode you want based on whether you passed arguments.

Background

The constraint that shapes everything is the pen carousel: you have a fixed, real set of pens, and the output has to commit to those colors. So color reduction isn't a nice-to-have -; it's the whole problem. The second constraint is time: plotters are slow, and a path that wanders all over the page wastes minutes lifting and repositioning the pen, which is where the path optimization earns its keep.

How It Works

The pipeline is built in Python around OpenCV, scikit-learn, scikit-image, SciPy, Shapely, NetworkX, and svgwrite. K-means finds the dominant colors in the image (analyzing more clusters than needed for diversity, then matching down to the selected pens). Each color region is then rendered in one of seven styles -; contour, hatch, crosshatch, stipple, spiral, line-art (Canny edges), or Voronoi cells with Lloyd's relaxation.

# seven drawing styles, one converter
contour    # outlines of color regions
hatch      # parallel lines at an angle
crosshatch # crossed parallel lines
stipple    # dots for shading
spiral     # spiral fills
lineart    # Canny edge detection
voronoi    # organic cell patterns (Lloyd's relaxation)

Once the strokes exist, optimize_path_tsp() groups paths by color and uses a TSP approximation (via NetworkX) to minimize pen travel, with a nearest-neighbor sort as the fallback. Output is written in millimeters for plotter compatibility -; one file per pen color (name_color1_HEX.svg) plus a combined SVG. The GUI runs conversions on a worker thread so it stays responsive and offers live previews of style changes.

Current Status

Archived as a Summer 2025 build -; and a genuinely complete one. The converter, both interfaces, all seven styles, color separation, and TSP optimization are all in place. The notes list obvious next steps (G-code export, plot-time estimates, batch processing) that were never pursued; it's parked as a working tool rather than an abandoned one.

  • Seven drawing styles with K-means color reduction matched to available pens.
  • TSP path optimization plus per-color SVG export and a combined file.
  • GUI and CLI from one converter; enhancement ideas (G-code, batch) left on the shelf.