Skip to main content
GAPoly logo

GAPoly

PyPI Python Pages Publish Gallery

GAPoly = GA + polygons (distribution carmelolg-gapoly, import carmelolg.gapoly, command gapoly).

Genetic algorithm that evolves elements until, stacked, they look like a target photo.

Gallery: https://carmelolg.github.io/GAPoly/

The element is chosen with --shape: polygon (default), disc, voronoi, mosaic, glyph or stroke. Built on GALib (carmelolg.galib, on PyPI as carmelolg-galib).

  • Genome: float32 array, one row per element, drawn in row order. A polygon has 3-6 vertices and a colour (RGBA), a disc a centre, a radius and a colour, a Voronoi seed a position and a colour, a mosaic tile a centre, a radius and a colour, a glyph a centre, a size, an angle, a character and a colour, a stroke a Bezier curve (start, control point, end), a brush width and a colour.
  • Fitness: pixel MSE between render and target (lower is better, maximize = False).
  • Mutation: move or reshape an element (a vertex, or the radius of a disc), change colour/alpha, add / remove an element, change overlap order.
  • Crossover: swap tails of the two element lists.

Shapes

--shape Element Genome row Default --elements
polygon 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

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, and shapes/__init__.py registers them by name (the values of --shape). The problem, the callbacks and the command line only use the interface (see "Project layout"). A disc is never smaller than the pixel under its centre, and its radius is a fraction of the longest image side, so discs stay round at any resolution. A mosaic tile is an opaque regular hexagon with no alpha. The rasteriser rounds its radius to a size level (steps of the square root of two) and moves its centre to the nearest node of the honeycomb lattice of that level, so tiles of one size fit together with no gaps or overlaps, big tiles cover regions and small ones add detail; a position change smaller than half a cell does not change the image. A glyph is 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). A stroke is 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 / --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. 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.

Polygon genome layout

A genome is a float32 array, one row per polygon, drawn in row order (later rows on top). 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; test_row_layout_is_the_documented_one fails if the columns change.

Setup

pip install carmelolg-gapoly           # from PyPI
pip install "carmelolg-gapoly[live]"   # plus the live matplotlib window

From a clone (development):

python -m venv .venv && source .venv/bin/activate
pip install -e ".[live,dev]"

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/test_gapoly.py

To add a shape: write a module in shapes/ with the layout, kernel, operators and a Shape subclass, and add it to shapes/__init__.py.

Run

python -m carmelolg.gapoly photo.jpg --generations 5000 --live          # live window
python -m carmelolg.gapoly photo.jpg --snapshot-every 1 --generations 300  # one PNG per generation

Output in out/<name>/: snapshots/gen_*.png, final.png, evolution.gif, history.csv. Elements shrink coarse-to-fine: from --start-size (0.5) to --max-size (0.12, fraction of image side) over the first 70% of the run. The live window lays out its panels by photo orientation: stacked for landscape, side by side for portrait/square. Rendering and error are one compiled Numba loop that releases the GIL, so fitness runs in parallel with GALib workers= in a thread pool: --workers N (default all CPUs). Useful options: --shape, --elements, --population, --mutation-rate, --size (working resolution: smaller = faster), --seed.

Tests

python -m pytest

Ideas not done yet

Region weights (face counts more), SSIM fitness, greedy hill-climbing baseline.

Metadata

Release files for carmelolg-gapoly 0.3.1

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.1
File Size Uploaded Provenance Actions
carmelolg_gapoly-0.3.1.tar.gz 45.8 kB With provenance Details for carmelolg_gapoly-0.3.1.tar.gz

Built distribution (wheel)

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

Total release size: 85.3 kB

Release files / carmelolg_gapoly-0.3.1.tar.gz

Download URL carmelolg_gapoly-0.3.1.tar.gz
Size 45.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e16547d2e9ac3a579b522b76cf65c919c5ffe37a948b978184e658dc68a21f01
BLAKE2b-256 checksum
How to use checksums
f4937adc7679bad48fc9676ab61e3ba5d4021518f96084c05172277c5bba973d
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.1-py3-none-any.whl

Download URL carmelolg_gapoly-0.3.1-py3-none-any.whl
Size 39.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e77fcd2b6bfb08535473bc944631fb254f7afd18ff552e2d2aa51d410a8d8cb
BLAKE2b-256 checksum
How to use checksums
46ba0f8bd79713abb9de4a7522716107bae29769450377749244ef8c530dbc76
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.1

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