An Algorithm For Art: Thread Portraits

We’ve all been there — through the magic of the internet, you see someone else’s stunning project and you just have to replicate it. For [Jenny Ma], that project was computer-generated string art, as in the computer figures out the best nail order to replicate a given image, and you lay out the thread yourself.

So, how does it work? Although a few algorithms are out there already, [Jenny] wanted to make her own using Python. Essentially it crops the image into a circle and then lays out evenly-spaced software nails around the circumference. The algorithm starts from a random nail and then determines the best next nail to wrap around by drawing a line from that nail to every other nail and choosing the darkest one based on the darkness of the image underneath that little line. It repeats this one chord at a time, subtracting from the original image until every pixel has been replaced with a thread or lack thereof, and then it spits out an ordered list of nail numbers.

Once the software was ready, [Jenny] made a wood canvas that’s 80 cm (31.5″) in diameter and started laying out the nail hole locations. There wasn’t quite enough room for 300 nails, so instead of starting over, [Jenny] changed the algorithm to use 298 nails and re-ran it.

[Jenny] does a great job of discussing the many variables at play in this hardware representation of software-created art. The most obvious of course is that the more nails used, the higher the resolution would be, but she determined that 300 is the sweet spot — more than that, and the resolution doesn’t really improve. We have to wonder if 360 nails would make things any easier. Check out the build video after the break.

Want to cut out most of the manual labor altogether? Build yourself a string art machine.

27 thoughts on “An Algorithm For Art: Thread Portraits

  1. What a great project, it came out fantastic. Thank you for explaining how this was done. This is a real cool math -art- computer programming project. thank you for sharing.

  2. Upon seeing this, my first thought was, how could you get a spider to run the pattern?
    Kind of looks like you had a trained spider do the leg work and then shook carbon or toner over the web afterwards.
    It also makes me wonder how long you could have the nails before they tend to bend too much. Some offset in the “layers” and see what the shadows do as the light moves.
    Then that leads me to the idea of an outdoor one on the scale of a corral.
    Use tall poles like a batting cage. Bonus points for making it work as a sundial also.

    lol. Can you tell that I like the project?

  3. Amazing work.

    Two thoughts for automation that are less extreme than the linked CNS machine:

    1) create an annulus with LEDs corresponding to the nail positions, and light up the next LED on cue to aim the next thread

    2) have a rotary table that can index under stepper/geared control, to bring the next nail to you, with suitable thread tensioning facilities

  4. Interesting project and beautiful art.

    Something about this reminds me of CT scanners and the algorithms used to construct image slices.

    In essence they are similar tasks, both express an image, related to density, as expressed with straight lines, though one goes from (an x-ray) thread to an image, while the other translates an image to (polyester) threads.

    1. I was thinking the same thing, in that it is a kind of reverse computed tomography, but the difference is that beams can go through a pair of points on the perimeter that are not diametrically opposite, unlike the beam in a CT scanner, which rotates around and passes through the centre.

  5. I think the author did come across the Hough transform, **the** classic image processing algorithm to answer the question “if I were to represent an image through straight lines, what’s the least-error method of doing so”, from 1959 (it’s really classic, I think it’s typical intro-textbook level material to image processing):

    Sure, there’s research papers on applying that specifically to string art (most of them aren’t *that* good), but the algorithm is simple enough and very close to what she’s doing. If she didn’t stumble across the Hough transform: Kudos, it’s a clever thing to do, and her execution is nice!

    1. @Marcus Thanks for introducing the Hough Transform. I read the Wiki article and I think I understand it. However, I am not able to see how to use that algorithm (in the modern form) to the string-art problem. Help?

  6. Beautiful work.

    Here’s an idea: Do this with a negative of the picture you want, then display it using a Vectrex-like (or early Atari vector-based game engine -like) display. I realize that drawing thousands of vectors would not likely be flicker-free, but let’s call that a feature, since the image would be constantly changing, while integrating in our vision to the desired image.

    Here’s another idea: instead of thread, use Kapton tape, like around 10mm wide. Then you CAN use the transparency of the “thread”, and it would certainly make a difference.

  7. Not a hack, interesting in a limited sort of way but that is all. Not sure how I feel about people ripping off existing artists, they usually have a tough enough life as it is.

    1. I personally do not think this is about ripping people off. Petros Vrellis, the artist she is referring to, seems like a pioneer to me in this kind of art. And he inspired many people to give this one a try for themselves (including me). Nice to see that also other managed to figure out an algorithm to create such art.

      For anyone who is interested, see here the website of Petros Vrellis:

      I have also posted this kind of art as a DIY-Project on Instructables last year:

      1. Intent and effect are two seperate things. I’d be more inclined to call it a hack if the code would work with any closed shape and or allowed tricks such as two strings being able to form a twist that allowed X lines to form. Genuine innovation.

  8. I would also have used the thickness of the nails in the algorithm. That doubles the “resolution” without any extra work. It would just spit out if you have to wrap the thread clockwise or counter clockwise around the nail.

    1. In addition to taking into account the thickness of the nail (and therefore determining which ‘side’ of the nail to land on), I wonder if it also matters whether the ‘target nail’ is ‘straight ahead’ (across the center of the board from ‘current nail’) vs at a sharp angle. The thread is resting on a curved cylinder (the nail) and I wonder if the departure point – which is dictated by the angle of the thread – could affect the accuracy. Would be fun to figure this out!

  9. Hi, im from colombia, my english isnt the best, but i need the algorithm, this is string art or in spanish “hilorama” i make those things in my free time, i wanna do this type of portraits but i need the algorithm, do you even know wera i can found it?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.