MCPcopy Index your code
hub / github.com/armhold/polygen

github.com/armhold/polygen @v1.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.0 ↗ · + Follow
91 symbols 214 edges 12 files 31 documented · 34%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Polygen uses a genetic algorithm to approximate an image with a small number of polygons.

You can generate a fairly good approximation with surprisingly few polygons. Here's a sample that has only 50 polygons (~380K generations):

starry-night starry-night 50 polygons

This one is 100 polygons:

mona-lisa mona-lisa 100 polygons

A more challenging image- 500 polygons & 680,850 generations:

revolver revolver 500 polygons

The algorithm is pretty simple:

  1. Create an initial string of candidate "DNA" consisting of a set of polygons (a color and a set of points) via random number generation.

  2. Render the DNA to an image (the "phenotype"). Compute its fitness by comparing to the reference image.

  3. Apply random mutations to the candidate (change color, move polygon points, juggle the z-order) to create a population of offspring.

  4. Evaluate the offspring, and if their fitness is better, replace the parent.

  5. Repeat for N generations.

Usage

  1. $ go get github.com/armhold/polygen/...
  2. $ cd $GOPATH/src/github.com/armhold/polygen
  3. polygen -source images/mona_lisa.jpg -poly 50
  4. Let it run until you are happy with the output (in output.png), or until you notice that there is not much change between generations.

Polygen includes a built-in web server, so you can watch the image evolve in more or less realtime. Just point your browser to http://localhost:8080.

logo

Credits

This code is my own, but credit goes to Roger Johansson for the original idea, which he documented here.

The file "mona_lisa.jpg" contains a low-resolution portion of the painting Mona Lisa, by Leonardo da Vinci. It is in the Public Domain.

The file "starry.jpg" contains a low-resolution copy of the painting The Stary Night by Vincent van Gogh. It is in the Public Domain.

The file "Revolver.jpg" contains a low-resolution copy of the cover art for the album Revolver by the artist The Beatles. The cover art copyright is believed to belong to the label, Parlophone/EMI, or the graphic artist(s), Klaus Voormann. It is included under Fair Use.

Core symbols most depended-on inside this repo

Compare
called by 7
image.go
Print
called by 5
stats.go
FastCompare
called by 4
image.go
ConvertToRGBA
called by 4
image.go
sqDiff
called by 4
image.go
copyOf
called by 4
candidate.go
randomCandidate
called by 4
candidate.go
randomPoint
called by 3
candidate.go

Shape

Function 54
Method 25
Struct 11
TypeAlias 1

Languages

Go100%

Modules by API surface

candidate.go24 symbols
bindata.go20 symbols
server.go8 symbols
image_test.go7 symbols
evolver.go7 symbols
image.go6 symbols
stats.go4 symbols
safe_image.go4 symbols
candidate_test.go4 symbols
util.go3 symbols
util_test.go2 symbols
cmd/polygen/main.go2 symbols

For agents

$ claude mcp add polygen \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact