Creative Coding

Getting Started With String Art Generation Algorithms

Turning a photo into a set of straight lines between pins on a circle sounds like it needs something clever. The classic approach is a greedy algorithm you can write in under fifty lines.

String art — an image approximated by straight thread stretched between pins arranged around a frame — is a real handicraft tradition that also happens to be a clean, approachable generative-art algorithm once you frame it right. The classic single-color version is something you can build yourself in an afternoon, and it’s a good first project for understanding greedy optimization in a visual domain.

The core idea

You’re approximating a grayscale target image with overlapping dark lines on a white (or clear) background. The trick that makes this tractable is greedy, one-string-at-a-time optimization: start from some pin, look at every candidate line to every other pin, and lay down whichever one most improves the match to the target image. Then repeat from the new pin. No global solver, no backtracking — just “what’s the best single move right now,” over and over, for however many strings you’ve budgeted.

The algorithm, in outline

  1. Place pins evenly around a circle (or the boundary of whatever shape you’re using).
  2. Render the target image into a matching circular crop, converted to grayscale, and track a running “current canvas” starting pure white.
  3. For each step: for every candidate line from the current pin to any other pin, compute how much darkness that line would add along its path, and how much that improves the match against the target where the line would actually land.
  4. Pick the line that most improves the match. Draw it (accumulate its darkness onto your working canvas), move to the pin at its far end, and repeat.
  5. Stop after a fixed string budget, or once no candidate line improves the match anymore.
import numpy as np

def line_pixels(p1, p2, size):
    # Bresenham-style sampling between two pin coordinates
    length = int(np.hypot(*(p2 - p1)))
    return np.linspace(p1, p2, length).astype(int).clip(0, size - 1)

def best_next_pin(current, pins, target, canvas, visited_pairs):
    best_score, best_pin = -np.inf, None
    for i, p in enumerate(pins):
        if (current, i) in visited_pairs:
            continue
        coords = line_pixels(pins[current], p, target.shape[0])
        ys, xs = coords[:, 1], coords[:, 0]
        # darkness this line would remove from the target's remaining error
        score = (255 - target[ys, xs]).astype(float).sum() - canvas[ys, xs].sum()
        if score > best_score:
            best_score, best_pin = score, i
    return best_pin

That’s the whole algorithm. Everything else — anti-aliasing the line draw, decaying the darkness contribution so strings don’t just pile up on the darkest spot forever, enforcing a minimum chord length so the solver doesn’t waste strings on near-tangential pin pairs — is tuning on top of this loop, not a different algorithm.

The knobs that actually matter

  • Pin count. More pins means finer angular resolution but a slower per-step search (you’re checking every pin pair). 150–300 pins is a common sweet spot for a face-sized portrait.
  • String budget. This is your “resolution” in the output-complexity sense — too few strings and the image reads as abstract scribble, too many and fine detail gets muddy as lines overlap and darken areas that should stay light.
  • Darkness-per-string weighting. Real thread has a fixed opacity; simulate that rather than letting each string subtract an unbounded amount, or your generator will over-converge on a handful of pin pairs instead of spreading detail.
  • Minimum chord length. Without one, the greedy search happily spends strings on nearly-overlapping short hops between adjacent pins, which looks like noise rather than structure.

Where it goes from here

The single-color, single-layer version above is the Petros Vrellis-style classic — famous from the viral portrait-thread installations a decade ago — and it’s a complete, satisfying weekend project on its own. Multi-color, multi-layer variants, where the string paths are optimized for structure rather than left to fall out of pure greedy search, are an active small research area worth a separate look once the single-color version leaves you wanting more control over the result.