About String Art
String Art turns a picture into instructions for a classic piece of craft: nails around a circular board and one black thread wound from nail to nail, thousands of times, until the crossings of the thread draw the picture. The app works out the order of the nails and shows what the finished piece will look like.
How it chooses the lines
The picture becomes a map of how dark each spot must be. Starting from one nail, the app tries every line it is allowed to wind next and keeps the one that brings the simulated board closest to that map, then carries on from where that line ends. Thread darkens the board the way real thread does: each pass hides a fraction of whatever white is still showing, so crossings get darker but never more than black. It stops when it reaches the most lines you allow, or when no line improves the picture any more.
The numbers
RMSE measures how far the simulated board is from the target, pixel by pixel (0 is a perfect match). SSIM compares local brightness, contrast and structure (1 is identical); because thread is a fine texture of lines, the app also gives SSIM after a slight blur, closer to what you see from a few steps away. The thread length is the sum of all the lines, with 10 % more for winding round the nails and slack.
The maths behind it
Here is every step the app takes, with the equations it uses. You only need powers, square roots, sine and cosine, and the sign Σ, which means “add these all up”.
1. From a photo to a map of darkness
A photo stores each pixel as three numbers between 0 and 1, for red, green and blue. But these numbers are “squashed”: screens store light along a curve, so that dark shades get more of the numbers. Before doing any maths with light, the app undoes the curve to get the real amount of light L from a stored value v:
Our eyes find green much brighter than red, and blue the darkest of the three, so the brightness Y of a pixel is a weighted average:
The thread is black, so what the app needs is the opposite of brightness: how dark each spot must be. That is the target darkness T, from 0 (white, no thread) to 1 (black, fully covered):
Why undo the curve? Because thread works with real light: a spot half covered by black thread sends back half the light, and only real-light numbers add up like that.
2. Where the nails go
There are N nails evenly spaced on a circle of radius R around the centre (cx, cy). Nail 0 is at the top and the numbers go clockwise. Nail i sits at the angle
and its position comes from cosine and sine. On a screen y grows downwards, which is what makes the numbering run clockwise:
A line between two nails that are d places apart is a chord of the circle. On a board of diameter D it is this long:
For example, with 240 nails on a 600 mm board, nails 0 and 120 are opposite each other: sin(90°) = 1, so the line is the full 600 mm. Nails 0 and 60 are a quarter of the way round: 600 · sin(45°) ≈ 424 mm.
3. Drawing a line on a grid of pixels
A straight line rarely passes exactly through the middle of pixels. The app uses Xiaolin Wu's method: it walks along the line one column at a time, and where the line passes between two pixels, it shares the ink between them. If the line is a fraction f of the way from the centre of the upper pixel to the lower one, they get
so each column always gets exactly one pixel's worth of ink. But a slanted line is longer than the number of columns it crosses: a 45° line across 100 columns is 141 pixels long. So each line gets a correction
4. How thread darkens the board
One pass of thread covers only part of a pixel. At the working size a pixel is 600 mm / 700 ≈ 0.86 mm wide and the thread is 0.25 mm thick, so one pass covers about this fraction of it (the darkness factor, a setting, lets you make thread count as darker or lighter):
On a pixel of a line with ink share w, one pass covers a = α · k · w (never more than 1). Now the key idea: a new thread lying on top of old ones hides the same fraction of whatever white is still showing. If C is how dark a pixel already is, the white still showing is 1 − C, and after one more pass
After n passes over a white pixel the darkness is therefore
With a = 0.29: one pass gives 0.29, two give 0.50 (not 0.58), three give 0.64, ten give 0.97. Crossings get darker and darker but never darker than black, just like real thread.
5. Choosing the next line
The app measures how far its simulated board C is from the target T with the squared error: for every pixel, take the difference, square it (so too dark and too light both count as positive), multiply by how important that pixel is (W, from the brush: 1 normally, up to 3 for important areas), and add it all up:
Winding one more line makes each of its pixels darker by δ = a · (1 − C). How much does that lower the error? Working out (T − C)2 − (T − C − δ)2 gives the improvement the line brings:
A pixel helps while it is still lighter than it should be by more than half of δ; once it is dark enough, more thread there counts against the line. From the nail the thread is at, the app works out ΔE for every line it may wind next (about 200 of them), winds the one with the biggest ΔE, and repeats from the other end of that line.
This is called a greedy method: it always takes the best next step and never looks further ahead. Why not find the best sequence of all? With 200 choices at each of 3,000 steps there are 2003000 possible sequences, a number with about 6,900 digits. No computer could try them all; the greedy method needs only 200 × 3,000 = 600,000 scores, which takes a few seconds.
6. When to stop, and favouring short lines
The app stops after the most lines you allow, or when the best ΔE has stayed below a small threshold ε (normally 0) for 20 lines in a row: then no line helps any more, and those last 20 lines are taken out again. The length penalty β divides each line's score by its length ℓ raised to a power:
With β = 0 nothing changes; with β = 1 a line twice as long needs twice the improvement to win, which gives short lines near the edge more of a chance.
7. Measuring the result
The root-mean-square error averages the squared differences over all n pixels in the circle and takes the square root, so it is in the same units as darkness (0 is perfect):
SSIM (structural similarity) compares small 7 × 7 patches of the two pictures: their average darkness μ, how much they vary (σ), and how much they vary together (σTC). It is 1 when the patches match and close to 0 when they have nothing in common:
The small numbers c1 and c2 stop it from dividing by zero on plain patches. Close up, thread is a tangle of thin lines, so the app also gives SSIM after blurring both pictures a little, which is closer to what you see from a few steps away. Finally, the thread you need is the sum of all the line lengths from step 2, plus 10 % for going round the nails and for slack.
8. Adjusting the picture
Contrast and brightness work on the stored value v: the contrast s stretches it away from middle grey, the brightness b shifts it, and the gamma γ bends it, darkening the middle tones when γ is above 1:
The local contrast boost (CLAHE) cuts the picture into an 8 × 8 grid of squares. In each square it counts how many pixels have each shade (a histogram) and gives every shade a new value: the fraction of the square's pixels that are darker than it. Shades that many pixels share get spread apart, so detail appears in flat areas. Each count is capped at a limit first, so that noise is not blown up, and neighbouring squares are blended so that no seams show.
Your pictures stay with you
Everything runs in your browser. Pictures are never uploaded.
What it does
Version 1.0. Prepare the picture — crop, zoom and turn it, adjust brightness, contrast and gamma, boost local contrast, and paint what matters — with a check that warns about pictures that will not work well; choose a preset or set every parameter; and watch the thread go on. Then print the nail template at true size and the winding instructions, and wind the piece with the threading mode, which shows (and can say) the next nail and remembers where you are. Your settings and last result are remembered in this browser.