Skip to main content

gapoly

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

Genetic algorithm that evolves semi-transparent elements until, stacked, they look like a target photo. The element is chosen with --shape: polygon (default), disc or voronoi. 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.
  • 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

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 and VoronoiShape, 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. 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
  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.2.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.2.0
File Size Uploaded Provenance Actions
carmelolg_gapoly-0.2.0.tar.gz 26.7 kB With provenance Details for carmelolg_gapoly-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 54.4 kB

Release files / carmelolg_gapoly-0.2.0.tar.gz

Download URL carmelolg_gapoly-0.2.0.tar.gz
Size 26.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7878b5230db9cb01a4405203d222860151a925e34bf91c975cce1d3dc43a0c23
BLAKE2b-256 checksum
How to use checksums
0c1ac94de711bf635c72b14a8f4d1fa85a692418b8a230abef0e93cfa25e9ace
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 18, 2026.

Transparency log

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

Download URL carmelolg_gapoly-0.2.0-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6e1cf39a4900393b01b46f8c195bc8ecdbf4b7ca096329de4243fb0dadf2f95
BLAKE2b-256 checksum
How to use checksums
181e1792392609ccdabc6ba98cbd2e6bbc8c5c4d47f6d9c68263cfe6eda94a3c
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 18, 2026.

Transparency log

Security for carmelolg-gapoly 0.2.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.2.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 18, 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