# 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. Walt says:

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. smellsofbikes says:

This is an amazing project, from conception through implementation to the final product. What a great combination of code, hardware, and aesthetics.

3. cdn/Cloudflare ("just a moment") hater says:

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?

4. e says:

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

5. Observer says:

Interesting project and beautiful art.

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. e says:

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.

1. BrightBlueJim says:

I think it could work with only threads that go through the center, the main problem being that each thread needs to be a different thickness or transparency.

6. Marcus says:

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): https://en.wikipedia.org/wiki/Hough_transform

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. P says:

@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?

2. @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 apply that to the string-art problem. Help?

7. I wonder if one could just feed the thread through the nozzle of a 3D printer, and reuse it for this. Though many printers are a bit on the small side for this purpose.

1. Daniel says:

3D printers are rather good at making lines of plastic string when wrongly configured, maybe you could actually use the stringing effects to do this with plastic?

2. Stephan says:

Actually, why thread? Use a filament / settings prone to stringing ;)

8. BrightBlueJim says:

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.

1. BrightBlueJim says:

Similar, but that’s cheated – most of the tape strips don’t run the whole length.

9. Mike says:

I’m patient and can do very tedious things. But that’s a lot of string. Bravo. I wonder what effect backlighting could have, might increase the contrast.

10. 𐂀 𐂅 says:

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: http://artof01.com/vrellis/

I have also posted this kind of art as a DIY-Project on Instructables last year: https://www.instructables.com/DIY-String-Art-the-Lion/

1. 𐂀 𐂅 says:

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.

11. Daniel says:

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. Chris S says:

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!

12. Ivan naranjo says:

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?

1. I don’t think there‘s a thing like „the algorithm“. Only people, who tried and developed their own ones.