A C++ implementation of an algorithm to generate photorealistic string art images. See my blog post for details and more examples.
| Algorithm animation | Final result |
|---|---|
![]() |
![]() |
To compile simply run make.
The script can be run as
string_art input.pgm num_pins opacity threshold skipped_neighbors output.pgm
where
input.pgmis a square binary “P5” portable graymap image without comments (suggested512x512),num_pinsis the number of nails (suggested:256),opacityis the opacity factor, see below (suggested:0.2, higher value means brighter image),thresholdis the opacity factor, see below (suggested:255, higher value means brighter image),skipped_neighborsis how consecutive nails can be (suggested32),scale_factoris the scaling factor of the output image (suggested:8),outputthe output string-art image.
The copy-paste-able version
string_art input.pgm 256 0.2 255 32 8 output.pgm
I used ImageMagick to perform the conversions to/from .pmg format, it is as easy as
magick image.png image.pgm
Opacity and threshold affect the final black/white balance of the picture. Use the following image as a reference.



