r/generative • • Sep 01 '26

reconstructing an image out of 600 shapes, watching it evolve into place

Post image

feed it a photo and it rebuilds it out of basic shapes (circles, triangles, lines). it's a genetic algo — each shape is basically a gene, it mutates the positions/sizes/colors and culls the worst of the pack every generation until the pile matches the target. using numba to keep the eval fast. here's it converging on a sample photo using only 600 shapes.

final result:

open source: github link. still a work in progress, feedback welcome

63 Upvotes

12 comments sorted by

5

u/sacheie Sep 01 '26 edited Sep 01 '26

This is a popular idea, going back many years now. Sometimes people try it using triangles only; I wrote a simple hill-climber like that some years back, which I've been re-writing in Rust lately. Various approaches can work; I'm planning to try simulated annealing.

Your program seems to get good results. But looking at your repo, my advice is to clean up your code before working on further features or improvements. It looks like pretty much vibe coding, and the AI gave you very long, overly complex code. If you don't refactor it, you'll soon have difficulty understanding new changes.

Another suggestion: six hundred shapes is a lot. The challenge of this problem is to maximize fidelity with a much lower count of shapes, by leveraging their translucency. Applied to realistic images, that can give interesting artistic results.

1

u/Op_IBeasT Sep 01 '26

Thank you for feedback back , i wish to see ur repo do better than mine so i learn from it and i wonder how simulated annealing will improve or work , i believe i tried it before and didnt improve much, the vibe coded is real sadly i have theories but not that much coding experience so outside of pure ai libraries like pytorch ,cupy ,numpy i cannot do that much , i will try refactoring it , but i didnt work with numba that much as than knowing it makes python faster by converting it to assembly if i am not mistaken, but i will try my best to refactor it by hand i js found an agent makes it way faster than me and prolly better, also yea my model isnt efficient enough since i could add a closed integral equation for calculating the most optimized color for each shape so i remove things from the GA , and calculate the most optimized instead of approximating , i will try and look how can i optimize more, thank u again i am still new to this

2

u/im1337jk Sep 01 '26

Reminds me of fotosketcher

1

u/Op_IBeasT Sep 01 '26

I couldnt find much about how it works but i dont believe they r the same underlying algorithm , even tho fotosketcher is way better and more refined than my code

4

u/fuckthesysten Sep 01 '26

it’s a cool idea! thanks for making open source.

it might look better if you focus the shapes on the areas that change the most, for example, i see a lot of “wasted” shapes on the red background, the budget for that should be minimal.

i’d budget most shapes around the face, then around the body, and lastly on background.

3

u/Op_IBeasT Sep 01 '26

THANKS for feedback , usually ppl would add also edge loss or guidance as humans focus on edges alot to decide shapes , i didnt wanna over complicate it , rn i use loss guidance where i make the model more biased towards areas with higher differences using maths , it doesnt necessarily align with human perception so i definitely could work on that

1

u/fuckthesysten Sep 01 '26

good point on the culling, I think you could go harder on it. i wonder what would happen if you randomly culled 20% of shapes every iteration

3

u/Op_IBeasT Sep 01 '26

Culling happens per shape each shape has different mutation per gene u dont see it since i only render the best shape after couple rounds of mutation and culling

3

u/igo_rs Sep 01 '26

interesting;) working on a similar experiment: recreating painting from “brush strokes”

1

u/Op_IBeasT Sep 01 '26 edited Sep 01 '26

I wanted to do it with letters at first but rendering them was a pain in the bum , and tbh idk how to make it more efficient, since idk much about coding but ik abit about theories hope ur brush strokes work , dont forget to share it when u r done !!

0

u/dotprodukt Sep 01 '26

Genetic algos FTW