r/Python • u/Dirt2Master • Oct 13 '19
Made a genetic algorithm with the task of recreating an image using triangles. Used the pillow library for the image work and the rest was just math :)
42
u/humanitysucks999 Oct 13 '19
"just math"
6
23
u/chunkyasparagus Oct 14 '19
2
u/H_Psi Oct 14 '19
Step 1: Draw some circles around your points
Step 2: Draw the rest of the triangulation
9
u/tian2992 Oct 14 '19
Quite cool concept, reminds me of Primitive https://github.com/fogleman/primitive
5
u/Dirt2Master Oct 14 '19
i think thats just a better version of this π
Thanks for showing me , i'll see what i can use in this to improve mine.
i see this uses transparent rectangles. so :/
7
u/Steampunkery Oct 13 '19
Needs more triangles
2
10
6
3
u/sensual_rustle Oct 14 '19
Can you output this as svg images instead so we can get scaling resolution on the images?
3
u/Dirt2Master Oct 14 '19
it doesn't exactly matter with the resolution as the image would look practically the same. Sure, i'll look into it for ya.
the only reason why the best looks so messy is i ran it for 1 night, if i ran it longer it should look better.
2
u/sensual_rustle Oct 14 '19
My understanding is svg is based on vector math. That should scale really cleanly.
I COULD be wrong, but most the companies I've been working with have been moving to svg for design images over the past few years. Seems this would be a possible cool use case
3
0
3
3
3
2
u/TrainingShift3 Oct 14 '19
this is just awesome.
hope to be skilled enough to do something this cool someday
3
2
Oct 14 '19
How did you learn this/come up with this? Was it something you had seen or read about? Or what? How did you get started with something like this and even know where to begin? Just wondering. Thanks.
1
u/Dirt2Master Oct 14 '19
Well actually, i saw a project like this on scratch, so i looked at how it found out how to make an image with hexagonal shapes.
so using python, i came up with a genetic algorithm to create a new image off of a base image. Its goal is to match the sample image as best as possible so in 1 generation, it creates 10,000 random triangles, all random colors, and it chooses the triangle that best fits in the image and makes the "best image" have a value closer to the sample one. it repeats this process indefinitely
You should start by understanding what a Genetic Algorithm is. :)
4
u/DuckSaxaphone Oct 14 '19
Am I understanding correctly that you:
Create 10 000 random triangles
Choose one that best fits the image
Regenerate all the others
Repeat until convergence or you want to stop
Because that is an optimization algorithm that would get you a cool image (as can be seen in your post) but it's not really a genetic algorithm.
1
u/Dirt2Master Oct 14 '19
im pretty sure i made the program look at the best triangles color and shape, hen replicate it in the next generation with mutations, i havent looked at the code in a while as i just found out about r/python and made this program quite a while ago.
3
u/DuckSaxaphone Oct 14 '19
Ok, I'd be really interested to see your code if it's on github!
The classic way to do this problem with genetic algorithms is to treat whole images as the individuals in your population and triangles as genes (if we're doing the natural selection analogy). So you generate lots of images, keep the best x% and mix their triangles to make a new generation of images. Then repeat until you end up with a generation of images that all replicate the original.
I can see the logic of treating all your triangles as individuals and reproducing until the population as a whole best replicates the image but I think it wouldn't have the benefits of a genetic algorithm for this task.
1
u/Dirt2Master Oct 14 '19
Definitely , this was more of a test to see if i could do it so yeah.
2
u/DuckSaxaphone Oct 14 '19 edited Oct 14 '19
Totally, I don't mean to be overly critical but I thought you might be learning about genetic algorithms so I wanted to chime in.
Your result is cool and the algorithm is probably waaay more efficient than a genetic algorithm which can be very intensive to run for this problem. The upside of the GA would be that you'd be less likely to get stuck in a local minimum.
1
u/Dirt2Master Oct 14 '19
no insults taken, i have improved a lot since i created this program, this is an opportunity for me to look back and improve it :)
2
1
u/Dirt2Master Oct 14 '19
I released it on github: https://github.com/TyperOfCode/image_algorithm/
Feel free to use, experiment and improve the code :)
1
1
1
u/SushiSuki Oct 14 '19
Is that Blade from TT?
1
2
u/PrimaCora Oct 14 '19
I enjoy this corrupted looking art style, would love to see a game go crazy with it, or a developer go crazy trying to work with its hitbox/clipping bounds.
2
u/AVDelay Oct 14 '19
It is really cool, I would like to play with it. If you put it on github, please dm me sir.
2
1
1
Oct 14 '19
It'd be very interesting if you could make a fitness function for this that's not absolute error, since often in GA's we don't have the "answer" as we do in this example. For instance, if I was to add triangular noise and send you the noisy image without the target product, how do you define what's a "better" permutation of the pixels? Still very cool though, 10/10 would de-noise again!
0
Oct 13 '19
[deleted]
45
u/Dirt2Master Oct 14 '19 edited Oct 14 '19
sure, i would love to upload ot on github!
just my code is not exactly viewer friendly.
Hermits nest
Edit: https://github.com/TyperOfCode/image_algorithm/
:) feel free to edit and experiment.
2
u/ceomm Oct 14 '19
I want to contribute. I have to develop similar algorithm long time ago but with convex polygons
30
u/blackerbird Oct 14 '19
No one expects it to be perfect - we would love you to share how you have done it so that we can see what was used, what you did and learn from it. People appreciate it, they wonβt criticise you!
11
6
u/Pokemon-Master-RED Oct 13 '19
I too would like to see this on a repo somewhere. Would love to toy with it.
1
u/Dirt2Master Oct 14 '19
1
u/Pokemon-Master-RED Oct 14 '19
Awesome, thank you!
I do some illustrating, so I am planning to toy with this to come up with some fun color palettes.
1
1
u/stermister Oct 13 '19
Is there a standard library for Genetic Algorithms? In the past I've just recreated them
6
2
u/DenormalHuman Oct 14 '19
There are several, google 'python ga library'
However, you will need then to fit with their methodology and approach to GA's as a whole.
I find the basic principles behind GA so simple that it has always been easier to knock something together from scratch than often work to rework my problem to fit with the approach of a particular library.
0
Oct 14 '19
[deleted]
4
u/gunnerman2 Oct 14 '19
To be fair, this was a common assignment in the CS dept. at the school I went to. Our class got to do an adaptation of Karl Simms Evolving Virtual Creatures though, which was a lot of fun.
1
2
u/blargLeChien Oct 14 '19
yeah, I feel the same way.
I mean at this point every lineI write is in git, and I push most repo in github because why the fuck not? So why not post the link to begin with.
At the same time, I would need a non-troll reddit account before associating my github to reddit.
28
u/pynberfyg Oct 13 '19
What metric are you using to compare similarity?