so i saw an instagram post months back about this artist who made this beautiful portrait from
thread and i thought wow he must be incredibly talented to be able to do that by eye but more
recently i learned the real creator behind that portrait was the greatest artist of modern times
the computer but then i remembered wait i know how to code and so here we are in this video i'm
going to take you through the entire journey from creating a thread placement algorithm to
actually building out t he portrait thread by thread from scratch a work now known as an original jenny ma
and i want to give credit where credit is due to the original inventor of this concept
named Petros Vrellis thanks Petros! and if you're new here welcome i am a computer engineer who
fangirls a lot about airplanes in my spare time on my channel i typically make videos about the
engineering of different airplanes and how they fly but in this video there won't b
any flying objects involved well hopefully but enough talk let's get into the video
thanks to brilliant for sponsoring this video if you're looking for a fun way to brush up
on your math and physics skills without amount of textbooks involved then check out the link
in the description so let's start with step one that is writing the algorithm
there are already algorithms and research papers that people have written on this
but i wanted to see if i could make my own from scratch and if it would be better plus
this overachiever's trying to make some youtube content okay the algorithm needed to first take
the input image and crop it into a circle and set up the given number of nails to be evenly
spaced out around the perimeter of the circle yeah this process was like pretty complicated
but with that set up we can now start drawing threads between two nails on the board using
what is called a greedy algorithm and at each stage the algorithm will simply pick the best
nail for us to attach the thread to next so how do you actually determine which is the best nail
well initially we pick a random nail to start with and from the current nail we draw a line to
every other nail on the board and measure the darkness of the image underneath that line and
from those we take the darkest line and add it to our output image and subtract it from our
input image and repeat this process about five thousand times so sounds pretty simple right
yeah now there are a lot of variables at play here and changing one even slightly will affect all
of the others and drastically change the output image and here are some computer simulations with
each of these variables changing to show you the difference well one such variable is the number
of nails you place on the board this essentially will change the resolution of your end portrait
i experimented with 100 to 500 nails and i found somewhere around 300 nails was the magic number
any less and it was kind of hard to make out the image and any more didn't really improve
the resolution by much i also played around with the thickness of the thread relative to the width
of the entire portrait this is essentially the difference between drawing with thick wool yarn
versus very thin thread and as you might expect the thinner the thread the more defined and
lighter the picture ended up being another variable was the transparency of the string
you can think of this as having one black crayon to draw with versus having a few more shades of
gray as well but as you can see in the end results it didn't really make much of a difference so to
make my life easier i just set this value to 1 so that every pixel can only be black or white now
of course the input image that you feed into the algorithm is very important i decided to up the
contrast and i learned later on that darker images tend to turn out better results than lighter ones
and some images are just not great candidates like a lovely picture of this model that the algorithm
somehow turned into this devil incarnation and of course you can play around with the number of
iterations essentially the more thread you add the more details will appear but the darker your end
image will be plus after a certain point it will take much much longer with diminishing returns and
there are ways you can level up this algorithm too for example by working with rgb in colors instead
of just grayscale but i kind of like the aesthetic and last note if you're looking to implement this
algorithm yourself now i decided to take the average darkness of every line that is the sum
of the pixels underneath divided by the number of pixels and also skipped 10 of its nearest nails
so that the algorithm wouldn't just end up drawing a line to its nearest neighbor learned that the
hard way so with the algorithm finished i now had a list of numbers corresponding to the nails
where i need to place my thread and i an academic pass the torch for my second step manual labor
so i decided to make the end portrait around 80 centimeters in diameter i mean go big or
go well i am home but i can still go big now i wasn't able to find any circular
canvases that were big enough and reasonably priced so i decided to get a little crafty
so now might be a good time to tell you a little bit about my woodworking experience and that is
i've never cut a piece of wood in my life so it's a great point to start directly using
power tools right there's absolutely nothing that can go wrong with this well i didn't have to wait
long because turns out the clamps i bought were too small so decided to make a slight upgrade to
the greatest clamp of all my dad are dads safe to substitute as clamps? probably not but did we get
the job done? absolutely but first protection just kidding you obviously don't need a hammer for this
i also sanded down the side and the surface slightly to get rid of any splinters
circular canvas done can you see it
so guys my biggest concern in this process came true so originally i plan to place 300
nails on the board but as you can tell here i got to the end and i've only placed 298
and we've run out of room but the good thing here is it did end up being pretty much evenly spaced
throughout so i'm happy with that so i guess i have two options here i can either go back through
the entire board and try to place the nails a little bit closer together so i end up with
two extra spots or i can run the algorithm again but with 298 nails instead of 300
and since i prefer to leave the heavy lifting to the computer i'm going to go with the latter
option so fingers crossed we're going to end up with a somewhat similar results
but i think we're going to be fine all right let's just change this to 298 and we're off
and the waiting begins again so using the pencil markings i made
i proceeded to beat the devil out of the 298 nails
and finally with our algorithm complete and our canvas ready to go we can start threading
for the portrait i decided to go with a picture of my parents that they took for their marriage
i mean just look at my mom and that hair she is such an icon what happened
mom from my research it seemed like synthetic nylon or polyester threads
were both the strongest and the thinnest and it turns out my mom
already had a roll of polyester thread that she ordered off of aliexpress for probably
40 cents but we didn't know how long the actual thread was but i had to work with what i got
so about a thousand threads and a few hours in i was starting to panic a little bit because i
was supposed to get something looking like this but instead my board looked like this
but at this point i thought i was in way too deep now and i might as well finish i mean worst case
scenario i'll make it into an abstract piece and title it the scribbles of the seamstress or
something like that i mean modern art is all about finding beauty in absolutely nothing right also at
this point i realized the end picture was going to be flipped horizontally than the input image which
for a portrait like this didn't really matter but if i included any lettering that would have been
a concern thankfully around 2 000 threads in a semblance of a picture was starting to appear and
to be honest i already really liked the picture at this point it was very minimal and mysterious
also to be extra safe i ended up cutting and tieing the ends of the thread every thousand
nails or so just in case any accidents did happen it wouldn't unravel the entire thing
now i understand that originally this was supposed to be made from one single length of thread but i
wasn't about to take my chances don't tell anyone okay let's keep this between us also i did end
up running out of thread on the original roll but after making a quick stop at walmart i bought the
thickest roll of black polyester thread i could find and we were back in business and
five seasons of modern family later i saw my portrait come to life thread by thread
so
so guys that was my journey from start to finish building a portrait from thread
let me know in the comments down below what you thought of the end results and don't forget if you
like this video to click that thumbs up button and also subscribe to my channel for new engineering
content like this one now if you're wondering how i wrote this algorithm or you're looking to brush
up on your computer science skills yourself a great resource i'd recommend is brilliant
it's an interactive learning platform for topics in math science and engineering i thought their
course on computer science fundamentals was a great refresher on the basics i love how engaging
their content is i wasn't just giving sorting algorithms on paper but interesting examples
of how they're used in real life systems and quizzes to make sure i'm absorbing the content
and if you want to explore the topic of programming even more i'd highly recommend
their programming with python course the language i used to write my algorithm to learn more about
brilliance go to brilliant.org forward slash jenny ma and you can try it out completely free and if
you like what you see they're kindly offering a 20 discount of their annual plan for viewers as well
if you're interested in checking out the source code i wrote so maybe you can make one yourself
i'll be leaving a link to that in my patreon it's a great place to not
only support me as a creator but see some behind the scenes content of future videos
and connect with our engineering community of course you can also see more of jenny on my
instagram so that's it for me guys thank you so much for watching and i'll see you next time
hey