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), 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.

Metadata

Release files for carmelolg-gapoly 0.3.2

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.3.2
File Size Uploaded Provenance Actions
carmelolg_gapoly-0.3.2.tar.gz 46.1 kB With provenance Details for carmelolg_gapoly-0.3.2.tar.gz

Built distribution (wheel)

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

Total release size: 85.8 kB

Release files / carmelolg_gapoly-0.3.2.tar.gz

Download URL carmelolg_gapoly-0.3.2.tar.gz
Size 46.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b69fe73986f57e80b041d2bdb5de8c1b5e95ad234614d37c8dc6dc0d3f9c3923
BLAKE2b-256 checksum
How to use checksums
d2d44579f76ba2c348c0911421d723e37232a1ea11bb547f5a0ad752b99bc1cf
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 log

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

Download URL carmelolg_gapoly-0.3.2-py3-none-any.whl
Size 39.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a203e73d05cab524abd8198b1b272b6e032e94ded885e2f8ced5f7be15354d8
BLAKE2b-256 checksum
How to use checksums
b9e878e83c72e7818ca6b2e52dcfa5e804f646e82f35f7f6b24b0bb992d0e4ae
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 log

Security for carmelolg-gapoly 0.3.2

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