Skip to main content
GAPoly logo

GAPoly

CI Coverage PyPI Python

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-size and --max-size do 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. GLYPHS in shapes/glyphs.py lists 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, and fitness_from_parent:
                    a mutated child is scored by redrawing only the pixels its changes touch), works with any Shape
  core/             what every shape shares
    shape.py          Shape, the Strategy interface
    common.py         Limits, Genome / Row / Array types
    operators.py      crossover used by every shape
    kernels.py        Numba helpers: fill, MSE, windows, parent / child diff, and the shared part of the mutation
  shapes/           one module per way of reading the genome: layout, Numba rasteriser (it can draw a window of the
                    image), Numba mutation, 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 (see credits in each meta.json).

Metadata

Release files for carmelolg-gapoly 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for carmelolg-gapoly 0.4.0
File Size Uploaded Provenance Actions
carmelolg_gapoly-0.4.0.tar.gz 64.0 kB With provenance Details for carmelolg_gapoly-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for carmelolg-gapoly 0.4.0
File Interpreter ABI Platform Actions
carmelolg_gapoly-0.4.0-py3-none-any.whl Python 3 none any Details for carmelolg_gapoly-0.4.0-py3-none-any.whl

Total release size: 119.2 kB

Release files / carmelolg_gapoly-0.4.0.tar.gz

Download URL carmelolg_gapoly-0.4.0.tar.gz
Size 64.0 kB
Tags Source
SHA-256 checksum
How to use checksums
926a62ad5b80f114322fc5472e27932382c10386ce8745b415ebbe2f370ee871
BLAKE2b-256 checksum
How to use checksums
b3f4908def0895fbfeb4d462f13df8c748c780618df0ce4382a96a3413bc1356
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 21, 2026.

Transparency log

Release files / carmelolg_gapoly-0.4.0-py3-none-any.whl

Download URL carmelolg_gapoly-0.4.0-py3-none-any.whl
Size 55.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7384db6ed445dc0ebbfe8cbea7b70a4899f38b16941ad1cdda1d70779a91d3f
BLAKE2b-256 checksum
How to use checksums
ad46f5ea59db90812747be649b8a2a0a6d9cc61d4a25430cd19f682db2bd394e
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 21, 2026.

Transparency log

Security for carmelolg-gapoly 0.4.0

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.

Select from 2 files

Attestation for carmelolg_gapoly-0.4.0.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 21, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page