Skip to main content

Learn and apply deterministic color transforms from aligned image pairs

Project description

Chromapair

Chromapair learns a deterministic, reusable color transform from spatially aligned areas in a source image and a reference image. It exports either three 256-entry RGB lookup tables or a regularized RGB polynomial, selected by the lowest four-fold cross-validated CIEDE2000 error.

It is image- and layout-agnostic:

  • dimensions come from the input images by default;
  • a smaller source can be positioned inside a reference canvas;
  • one area or several disjoint areas can be fitted together;
  • averaging tile size is calculated automatically and edge tiles need not divide evenly;
  • output is stable, validated JSON that can be applied from the CLI or Python.

Install

Chromapair requires Python 3.11 or newer and is tested with Python 3.14.

uv tool install git+https://github.com/alixsep/chromapair.git
chromapair --help

After a PyPI release, uv tool install chromapair works as well. For development:

git clone https://github.com/alixsep/chromapair.git
cd chromapair
uv sync --extra dev
uv run pytest

Fit and apply

With no area, the full overlap between the source and reference is used:

chromapair fit \
  --source source.png \
  --reference reference.png \
  --name warmer

The reference retains its natural dimensions and the source starts at (0, 0). Apply the generated preset to the original source or another image:

chromapair apply \
  --preset output/presets/warmer.json \
  --input another-source.png \
  --output warmer-output.png

The output format is selected from its extension. RGB is transformed while dimensions and transparency are preserved. Existing outputs are protected unless --force is supplied.

Fit multiple areas

Coordinates use exclusive right and bottom bounds. Repeat --area as needed:

chromapair fit \
  --source texture.png \
  --reference proxy.png \
  --name violet \
  --canvas-size 813 1185 \
  --source-origin 28 28 \
  --area 35 204 67 844 \
  --area 71 1084 426 1109

--canvas-size resizes the reference with Lanczos resampling. The source is never resized or modified; --source-origin only establishes aligned coordinates. Every selected area must be covered by both images.

By default Chromapair aims for about 512 spatial samples:

sample size = floor(sqrt(total selected pixels / target samples))

Each area is tiled independently. Partial tiles along the right and bottom edges are averaged normally, so area dimensions do not need to share factors. Use --target-samples N to adjust the automatic tradeoff, or --sample-size N for an explicit, reproducible override.

Other useful options:

chromapair fit --help
chromapair fit ... --no-previews
chromapair fit ... --preset-output path/to/preset.json
chromapair fit ... --metadata '{"target":"frame"}'
chromapair apply ... --force

Batch configuration

Run all four checked-in examples with:

chromapair batch --config examples/batch.json --no-previews

Paths are relative to the config. areas accepts either one rectangle:

"areas": [35, 204, 67, 844]

or multiple rectangles:

"areas": [
  [35, 204, 67, 844],
  [71, 1084, 426, 1109]
]

Defaults are merged into every job. See examples/batch.json for the complete version-1 format.

The combined bundle produced by a batch can be applied directly:

chromapair apply \
  --preset examples/presets.json \
  --preset-name example-1 \
  --input examples/input-1.webp \
  --output result.png

Examples

These examples deliberately start with altered inputs and learn the inverse transform from each aligned reference. Every output below was produced by Chromapair using examples/batch.json and the checked-in presets.

Example Altered input Reference Chromapair output
1. Hue shift
Hue shifted by -150.
Blue hue-shifted hoodie input Pink hoodie reference Recovered pink hoodie output
2. Contrast, brightness, and size
Reduced to 80% contrast and darkened by 30%. The 64×64 reference is resized to the input's 512×512 canvas for fitting; the output remains 512×512.
Dark low-contrast glass input Glass sculpture reference Restored glass output
3. Saturation and inversion
Saturated by 50% and inverted.
Saturated inverted penguin input Penguin reference Recovered penguin output
4. Selective color shift
Pink foliage shifted toward purple.
Purple-shifted landscape input Pink landscape reference Recovered pink landscape output

Output

A single fit writes:

output/
├── presets/<name>.json
├── reports/<name>.json
└── <name>/
    ├── reference-areas.png
    └── selected-source.png

The preset contains the transform and reproducibility metadata. The report contains geometry, resolved sampling, all candidate errors, selection, and a SHA-256 of the canonical pretty-printed preset.

Chromapair compares an RGB histogram LUT with regularized degree 1, 2, and 3 RGB polynomials. Model selection uses averaged, spatially corresponding samples. Final histogram fitting and quality metrics use every selected visible pixel. Fully transparent source pixels are ignored. Source alpha is preserved in preview and applied output.

Presets are validated before they are written or applied. Invalid format versions, methods, LUT lengths, polynomial terms, coefficient shapes, and non-finite numeric values are rejected with a clear error.

Python API

import json
from pathlib import Path

from PIL import Image

from chromapair import apply_preset, validate_preset

preset = json.loads(Path("preset.json").read_text())
validate_preset(preset)

with Image.open("input.png") as image:
    output = apply_preset(image, preset)

output.save("output.png")

apply_preset returns a new image and does not modify its input.

Determinism

Chromapair has no random fitting step. Tiles, fold assignments, candidate ordering, coefficient rounding, JSON key ordering, and output hashing are deterministic. Lock dependencies with the checked-in uv.lock when exact cross-machine reproduction matters.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chromapair-1.0.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chromapair-1.0.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file chromapair-1.0.0.tar.gz.

File metadata

  • Download URL: chromapair-1.0.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for chromapair-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ab594428feae9fdab6b1130e21471c68ee620325094aae1df7c464e4b49648d8
MD5 5aac0fe2cf3e9e50f1da16ece77473b9
BLAKE2b-256 8f7f938541774272e83bb86692e28af77436400e19617acf16e785ba655369d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for chromapair-1.0.0.tar.gz:

Publisher: release.yml on alixsep/chromapair

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chromapair-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: chromapair-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for chromapair-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5406b366c6a28d1a4922e7c893f9ca0138c31ef93e81715413270b173bb244d
MD5 e186a65e5f18911bf7091ac40c308205
BLAKE2b-256 e0b973c2731bf6b019551dc2d8e1c8d0d605fe8db772706360a379276bd8ef57

See more details on using hashes here.

Provenance

The following attestation bundles were made for chromapair-1.0.0-py3-none-any.whl:

Publisher: release.yml on alixsep/chromapair

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page