|
|
GAPoly |
GA + polygons. A genetic algorithm, built on GALib, that evolves thousands of simple elements (polygons, discs, Voronoi cells, tiles, glyphs or brush strokes) until, stacked, they look like a target photo.
See what it makes in the gallery.
| Distribution | carmelolg-gapoly |
| Import | carmelolg.gapoly |
| Command | gapoly |
Quick start
pip install carmelolg-gapoly # from PyPI
pip install "carmelolg-gapoly[live]" # plus the live matplotlib window
gapoly photo.jpg --generations 5000 --live # live window
gapoly photo.jpg --shape voronoi --elements 1000 # pick the kind of element
gapoly photo.jpg --snapshot-every 1 --generations 300 # one PNG per generation
python -m carmelolg.gapoly works as well as gapoly. Everything is written to out/<name>/:
| File | What it is |
|---|---|
final.png |
the result |
evolution.gif |
how the image evolved |
history.csv |
the error per generation |
snapshots/gen_*.png |
one image per snapshot (with --snapshot-every) |
Useful options
| Option | Effect |
|---|---|
--shape |
kind of element, see Shapes |
--elements |
number of elements in the image |
--population, --mutation-rate |
genetic algorithm parameters |
--size |
working resolution: smaller is faster |
--workers N |
threads for the fitness (default: all CPUs) |
--seed |
reproducible run |
--start-size, --max-size |
size of the elements, see below |
Elements shrink coarse to fine: from --start-size (0.5) to --max-size (0.12), as fractions of the image side,
over the first 70% of the run. Rendering and error are one compiled Numba loop that releases the GIL, so the
fitness runs in parallel with GALib's workers=. The live window stacks its panels for landscape photos and
puts them side by side for portrait or square ones.
How it works
| Part | What happens |
|---|---|
| Genome | a float32 array, one row per element, drawn in row order (later rows on top) |
| Fitness | pixel MSE between the render and the target (lower is better, maximize = False) |
| Mutation | move or reshape an element, change colour or alpha, add or remove an element, change the overlap order |
| Crossover | swap the tails of the two element lists |
Shapes
The element is chosen with --shape.
--shape |
Element | Genome row | Default --elements |
|---|---|---|---|
polygon (default) |
polygon with 3-6 vertices | 17 columns, see below | 400 |
disc |
disc, from one pixel up to a group of pixels | 7 columns: x y radius r g b a |
2000 |
voronoi |
seed of a Voronoi cell: each pixel takes the colour of its nearest seed | 5 columns: x y r g b |
1000 |
mosaic |
opaque hexagonal tile snapped to a honeycomb lattice, in several sizes | 6 columns: x y radius r g b |
3000 |
glyph |
typographic character, rotated and coloured | 9 columns: x y size angle glyph r g b a |
1500 |
stroke |
brush stroke: quadratic Bezier curve painted with a round brush | 11 columns: x0 y0 cx cy x1 y1 width r g b a |
1500 |
Details of each shape:
- Disc: never smaller than the pixel under its centre. Its radius is a fraction of the longest image side, so discs stay round at any resolution.
- Voronoi: seeds have no size, alpha or drawing order, so
--start-sizeand--max-sizedo nothing for them. The rasteriser finds the nearest seed with a grid, exactly and in time close to the number of pixels: 20000 seeds on a 512x341 image take about 10 ms. - Mosaic: an opaque regular hexagon with no alpha. The radius is rounded to a size level (steps of the square root of two) and the centre moves to the nearest node of that level's honeycomb lattice. Tiles of one size fit together with no gaps or overlaps: big tiles cover regions, small ones add detail. A position change smaller than half a cell does not change the image.
- Glyph: drawn from an atlas of character masks, rasterised once with Pillow's font.
GLYPHSinshapes/glyphs.pylists the characters; the exact look depends on the font Pillow finds. - Stroke: a curve flattened into short segments. Every pixel nearer than half the brush width is covered once, so overlapping parts do not double the opacity.
For discs, mosaic tiles, glyphs and strokes, --start-size and --max-size cap the diameter, the glyph box and
the box around the three curve points. The brush is at most a third of that.
Polygon genome layout
Values are in [0, 1]; coordinates are fractions of the image side.
column: 0 | 1 2 | 3 4 | ... | 11 12 | 13 14 15 16
meaning: n | x0 y0 x1 y1 ... x5 y5 | r g b a
n is the number of vertices used (3-6); the rest are ignored. Do not index rows with bare numbers: use the names
in shapes/polygons.py (N_VERTICES, X(v), Y(v), RED..ALPHA, vertices, color, make_row).
describe(genome) prints a genome as text, one line per polygon. The Numba kernel in the same module reads the
same layout, and test_row_layout_is_the_documented_one fails if the columns change.
Development
python -m venv .venv && source .venv/bin/activate
pip install -e ".[live,dev]"
python -m pytest
Project layout
src/carmelolg/gapoly/
problem.py ImageProblem: the GALib Problem (fitness = MSE against the target), works with any Shape
core/ what every shape shares
shape.py Shape, the Strategy interface
common.py Limits, clamp, Genome / Row / Array types
operators.py mutation skeleton and crossover used by every shape
kernels.py Numba helpers (fill, MSE)
shapes/ one module per way of reading the genome: layout, Numba rasteriser, mutations, Shape class
__init__.py SHAPES, the registry (values of --shape)
polygons.py discs.py voronoi.py mosaic.py glyphs.py strokes.py
callbacks/ GALib callbacks: snapshots.py (PNG, CSV, GIF), live.py (matplotlib), schedule.py (size cap)
cli/ app.py (arguments, help text, main), help_format.py (colours)
tests/
site/ the gallery (published on GitHub Pages)
tools/ build_site.py, optimize_works.py
Each shape is a strategy: the abstract Shape (draw a genome, error against the target, create and mutate
elements, describe) is implemented by PolygonShape, DiscShape, VoronoiShape, MosaicShape, GlyphShape and
StrokeShape. The problem, the callbacks and the command line only use that interface.
To add a shape: write a module in shapes/ with the layout, kernel, operators and a Shape subclass, then
register it in shapes/__init__.py.
Ideas not done yet
Region weights (a face counts more), SSIM fitness, a greedy hill-climbing baseline.
License
- Code: Apache-2.0 © 2026 Carmelo La Gamba. You may use, modify and redistribute it, but you must keep the copyright notice, the NOTICE file and the license text, and mark the files you changed. The license does not grant any right to use the name "GAPoly" or the author's name as a trademark.
- Works (images generated by the algorithm:
final,evolution,history.csv): CC BY-NC-SA 4.0. Share and adapt for non-commercial use only, giving credit to "Carmelo La Gamba / GAPoly", linking the license, noting changes and keeping the same license (ShareAlike). The original photos and artworks used as targets are not covered: they keep their authors' terms (seecreditsin eachmeta.json).
Metadata
Release files for carmelolg-gapoly 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| carmelolg_gapoly-0.3.4.tar.gz | 53.2 kB | With provenance | Details for carmelolg_gapoly-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| carmelolg_gapoly-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for carmelolg_gapoly-0.3.4-py3-none-any.whl |
Total release size: 98.0 kB
Release files / carmelolg_gapoly-0.3.4.tar.gz
| Download URL | carmelolg_gapoly-0.3.4.tar.gz |
|---|---|
| Size | 53.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
23dbe100c2bf71665ce0345b281a6654b62b905fcb5b281081f937a185bf729b
|
|
BLAKE2b-256 checksum How to use checksums |
e0898bf8617f5b66844cef7cf849d346237c535296555447383c21ebc0b57f47
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / carmelolg_gapoly-0.3.4-py3-none-any.whl
| Download URL | carmelolg_gapoly-0.3.4-py3-none-any.whl |
|---|---|
| Size | 44.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
32ccfd28c6e6bd0e407f1fad4fb8e6d75d3ff9fc9a57c8c43fe00098433b57a8
|
|
BLAKE2b-256 checksum How to use checksums |
7bcd0f3d39b90def23b84454207726f257794fdb8f28beeeda633dd317be2b1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for carmelolg-gapoly 0.3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for carmelolg_gapoly-0.3.4.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.