Turning an image into a cross-stitch chart is colour quantization with a palette you don't get to choose. Every cell has to become a thread someone can buy, and for DMC stranded cotton the classic solid range is 447 colours. (The current catalogue is bigger. The palette is just data, so the code doesn't care.)
The whole job comes down to one question, asked once per cell: which thread is closest to this pixel? How you define "closest" decides whether a portrait looks like a person.
Nearest in RGB is not nearest to the eye
The obvious metric is straight-line distance between two RGB triples. It fails on faces, and the reason is specific. Skin is a low-chroma orange, so it sits near the grey axis. In gamma-encoded sRGB, a grey-green or grey-violet thread of similar brightness is often numerically as close as the correct tan that is a little darker. The eye disagrees: near neutral, a hue shift is far more visible than a small lightness step.
Two pixels from a portrait, each with the two best candidates. Thread RGB values are the community-measured approximations everyone uses, since DMC publishes no official sRGB numbers.
| Pixel | Thread | RGB distance | ΔE00 |
|---|---|---|---|
Cheek (200,158,146) | 3023 Brown Gray Lt (177,170,151) | 26.4 | 16.2 |
152 Shell Pink Med Lt (226,160,153) | 27.0 | 6.7 | |
Jaw shadow (150,122,102) | 3052 Green Gray Md (136,146,104) | 27.9 | 20.8 |
3772 Desert Sand Vy Dk (160,108,80) | 27.9 | 7.9 |
RGB calls both a coin flip and lands on the grey-green thread. CIEDE2000 puts that thread about 2.5 times further away. Repeat this for every shadow pixel and the face goes olive. The same mechanism can push pink-lit highlights toward the antique mauves and violets.
sRGB to Lab
CIEDE2000 works on CIELAB coordinates, so convert first: undo the gamma curve, go to XYZ, normalise by the D65 white point.
function rgbToLab(r, g, b) { const lin = v => (v /= 255) <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4; const R = lin(r), G = lin(g), B = lin(b); const x = (0.4124564 * R + 0.3575761 * G + 0.1804375 * B) / 0.95047; const y = 0.2126729 * R + 0.7151522 * G + 0.0721750 * B; const z = (0.0193339 * R + 0.1191920 * G + 0.9503041 * B) / 1.08883; const f = t => t > 216 / 24389 ? Math.cbrt(t) : (24389 / 27 * t + 16) / 116; const fx = f(x), fy = f(y), fz = f(z); return [116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz)]; } CIEDE2000
Plain Lab distance (ΔE76) already fixes most of the problem. CIEDE2000 adds corrections for lightness, chroma and hue, plus a rotation term for blues. They matter most at low chroma, which is where skin and stone and fog live.
// Sharma, Wu & Dalal (2005), with kL = kC = kH = 1 function deltaE2000([L1, a1, b1], [L2, a2, b2]) { const rad = Math.PI / 180, deg = 180 / Math.PI; const Cab = (Math.hypot(a1, b1) + Math.hypot(a2, b2)) / 2; const G = 0.5 * (1 - Math.sqrt(Cab ** 7 / (Cab ** 7 + 25 ** 7))); const ap1 = (1 + G) * a1, ap2 = (1 + G) * a2; const C1 = Math.hypot(ap1, b1), C2 = Math.hypot(ap2, b2); const hue = (b, a) => (b === 0 && a === 0) ? 0 : (Math.atan2(b, a) * deg + 360) % 360; const h1 = hue(b1, ap1), h2 = hue(b2, ap2); const dL = L2 - L1, dC = C2 - C1; let dh = h2 - h1; if (C1 * C2 === 0) dh = 0; else if (dh > 180) dh -= 360; else if (dh < -180) dh += 360; const dH = 2 * Math.sqrt(C1 * C2) * Math.sin(dh / 2 * rad); const Lm = (L1 + L2) / 2, Cm = (C1 + C2) / 2; let hm = h1 + h2; if (C1 * C2 !== 0) { if (Math.abs(h1 - h2) <= 180) hm /= 2; else hm = hm < 360 ? (hm + 360) / 2 : (hm - 360) / 2; } const T = 1 - 0.17 * Math.cos((hm - 30) * rad) + 0.24 * Math.cos(2 * hm * rad) + 0.32 * Math.cos((3 * hm + 6) * rad) - 0.20 * Math.cos((4 * hm - 63) * rad); const SL = 1 + 0.015 * (Lm - 50) ** 2 / Math.sqrt(20 + (Lm - 50) ** 2); const SC = 1 + 0.045 * Cm, SH = 1 + 0.015 * Cm * T; const RC = 2 * Math.sqrt(Cm ** 7 / (Cm ** 7 + 25 ** 7)); const RT = -Math.sin(60 * Math.exp(-(((hm - 275) / 25) ** 2)) * rad) * RC; return Math.sqrt((dL / SL) ** 2 + (dC / SC) ** 2 + (dH / SH) ** 2 + RT * (dC / SC) * (dH / SH)); } I checked this against the 34 reference pairs in the Sharma paper, and all match to four decimals. Do the same if you port it. The hue-averaging branches are easy to get slightly wrong, and a wrong version still returns believable numbers.
Matching a whole image
Convert the palette to Lab once, then cache by packed RGB, because photos repeat colours.
const palette = DMC.map(t => ({ ...t, lab: rgbToLab(...t.rgb) })); // [{ code, name, rgb }] const cache = new Map(); function nearestThread(r, g, b) { const key = (r << 16) | (g << 8) | b; let hit = cache.get(key); if (hit === undefined) { const lab = rgbToLab(r, g, b); let best = Infinity; palette.forEach((t, i) => { const d = deltaE2000(lab, t.lab); if (d < best) { best = d; hit = i; } }); cache.set(key, hit); } return hit; // index into palette } A 200 × 250 chart is 50,000 cells. Uncached, that is about 22 million ΔE00 calls, which took roughly four seconds in Node 24 on my laptop. With the cache it is rarely worth moving to a worker.
Keeping the chart stitchable
A perceptually accurate chart can still be miserable to stitch. Three passes fix most of that, all on a <canvas> with getImageData, so nothing is uploaded.
- Downscale first. Draw the photo onto a canvas whose pixel size equals the stitch count, with
imageSmoothingQuality = 'high'. One pixel is one stitch. Quantize after this step, never before. - Cap the thread count. Count how many cells each thread got, keep the top 30 or so, and re-map the rest to the nearest kept thread with the same ΔE00 function. Nobody wants to buy a skein for six stitches.
- Merge confetti. Error-diffusion dithering such as Floyd–Steinberg gives smooth gradients and also isolated single stitches, each needing its own thread start and stop. Use it at reduced strength or not at all, then run a despeckle pass:
// grid: Uint16Array of palette indexes, w × h function despeckle(grid, w, h, maxDE = 8) { const out = grid.slice(); for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) { const me = grid[y * w + x], count = new Map(); for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) { const nx = x + dx, ny = y + dy; if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue; const n = grid[ny * w + nx]; count.set(n, (count.get(n) || 0) + 1); } if (count.has(me)) continue; // has a same-thread neighbour const [top] = [...count].sort((a, b) => b[1] - a[1])[0]; if (deltaE2000(palette[me].lab, palette[top].lab) <= maxDE) out[y * w + x] = top; } return out; } The maxDE guard is the important part. A lone stitch that is close in colour to its neighbours is noise and gets absorbed. A lone stitch that is far away, such as the highlight in an eye, is detail and stays.
To compare your output with a finished converter, Cross Stitch Tool turns a photo into a symbol chart with floss numbers in the browser. Load the same image into both and look at the faces first.
