A paint-by-numbers template turns a continuous-tone photograph — which may contain tens of thousands of distinct colors — into a small, fixed set of flat color regions, each labeled with a number that maps to a paint. Producing a good template is fundamentally a color quantization problem: choosing a limited palette that best represents the original image, then dividing the picture into paintable areas. This guide explains the algorithms behind the process and how to get the best results.
What Color Quantization Actually Does
Color quantization reduces the number of distinct colors in an image while keeping it recognizable. The naive approach — posterization — simply rounds each color channel to a few fixed levels. It is fast but produces harsh, arbitrary palettes because it ignores which colors actually appear in the picture. A far better approach is clustering: look at the colors that are genuinely present and group them into a handful of representative clusters. This tool uses k-means clustering, the workhorse algorithm for this task.
K-Means and Why Each Color Is an Average
K-means partitions all the image's pixels into k groups so that each pixel belongs to the cluster whose center is nearest to it. The center of each cluster — called the centroid — is the mean of every pixel assigned to it. That is why every color in the output palette is literally the average of the pixels it represents: it is the mathematically optimal single color to stand in for that whole group. The algorithm alternates two steps until it settles: assign each pixel to the nearest centroid, then recompute each centroid as the average of its members.
Getting Disjoint Colors With K-Means++
K-means is sensitive to where its clusters start. Poor initialization can leave two clusters chasing nearly the same color while an entire hue goes unrepresented. To avoid this, the tool seeds the clusters with k-means++, which picks starting colors that are spread far apart — each new seed is chosen with probability proportional to its squared distance from the seeds already picked. The practical effect is a palette of colors that are as disjoint as possible, so a portrait does not waste three of its colors on almost-identical skin tones.
Why CIELAB Instead of RGB
Distances in raw RGB do not match human perception: two colors that are numerically close in RGB can look very different, and vice versa. This generator converts colors to the CIELAB color space before clustering. In CIELAB, the straight-line distance between two colors approximates how different they look to the eye, so "the most distinct colors possible" genuinely means visually distinct. The centroids are averaged in CIELAB too, then converted back to RGB for display and painting.
From Colors to Paintable Regions
Once every pixel has a palette number, the image is split into connected regions — contiguous areas sharing the same color. Real photos produce a lot of tiny specks along edges and in noisy areas, which would be impossible to paint and would clutter the sheet with unreadable numbers. The tool performs region cleanup: any region below a minimum area is merged into the neighbor it shares the longest border with. The remaining regions are outlined by tracing the exact boundaries between differently colored pixels, producing watertight vector paths where neighboring regions share a single, gap-free line.
Placing the Numbers
Each region needs its number in a spot where it clearly sits inside the shape, even for curved or L-shaped areas where the geometric center could fall outside the region entirely. The generator computes a distance transform for every region and places the number at the "pole of inaccessibility" — the interior point farthest from any border. The size of each number scales with the space available, so large sky regions get bold numbers while small highlights get modest ones.
Choosing the Right Number of Colors
- 2–4 colors produce bold, poster-like results and very easy templates — great for children or minimalist wall art.
- 6–10 colors hit the sweet spot for most photographs: recognizable subjects with a manageable number of paints.
- 12–18 colors capture portraits and landscapes with convincing shading and gradients.
- 20–64 colors yield gallery-quality detail but demand patience and a large canvas so the smallest regions stay paintable. Very high counts work best with the vividness boost so the added colors stay distinct.
The recommended value is a good starting point derived from an elbow analysis of your specific image, but the best count depends on your patience, your canvas size, and how many paints you have. Because switching color counts is instant, it is worth trying two or three values and comparing the previews before you print.
Printing and Painting Tips
Download the numbered template as SVG whenever possible: because it is vector, it prints crisply at any size, from a postcard to a meter-wide canvas. Print the color legend on the same page so you can mix or buy the matching paints. When transferring to canvas, larger prints make the small numbered regions much easier to fill. If some regions feel too fiddly, reduce the color count by one or two and regenerate — fewer colors means larger, simpler regions.