Metadata-Version: 2.4
Name: duotonics
Version: 1.1.0
Summary: Reproducible duotone image processing with OpenCV and NumPy, available as a Python CLI, library, and tkinter GUI.
Author: Michail Semoglou
License-Expression: MIT
Project-URL: Homepage, https://github.com/MichailSemoglou/duotonics
Project-URL: Repository, https://github.com/MichailSemoglou/duotonics
Project-URL: Issues, https://github.com/MichailSemoglou/duotonics/issues
Project-URL: Changelog, https://github.com/MichailSemoglou/duotonics/blob/main/CHANGELOG.md
Keywords: duotone,image-processing,opencv,numpy,color
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: End Users/Desktop
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Scientific/Engineering :: Image Processing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: opencv-python-headless<5,>=4.5.5
Requires-Dist: numpy<3,>=1.22
Dynamic: license-file

# Duotonics

[![tests](https://github.com/MichailSemoglou/duotonics/actions/workflows/tests.yml/badge.svg)](https://github.com/MichailSemoglou/duotonics/actions/workflows/tests.yml)
[![license: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/MichailSemoglou/duotonics/blob/main/LICENSE)

Duotonics is a Python application for reproducible two-color imaging. It blends random or explicit color pairs over a source image's tonal key in gamma-encoded sRGB (legacy), linear sRGB, or Oklab (Ottosson, 2020). It accepts JPEG, PNG, and BMP input and writes a batch of lossless PNGs (100 by default) plus `colors.csv` and `metrics.csv` sidecars. The application is usable through file dialogs, a command-line interface, or as a Python library.

## Features

- Image selection via a file dialog
- Generates 100 duotone variations as lossless **PNG** files (count configurable)
- Filenames use **hex color codes** in RGB order (e.g. `083_2CD2B4_DC1E5A.png`), ready to paste into Figma, Illustrator, or your design system
- Duotone, tritone, and quadtone output: two to four anchor colors interpolated over equally spaced tonal positions (tritone filenames add a third hex group, e.g. `000_000000_808080_FFFFFF.png`)
- Produces a **`colors.csv` sidecar** listing every variation's anchor colors and tone curve for easy reference and sharing
- Produces a **`metrics.csv` sidecar** reporting mean, median, and 95th-percentile CIEDE2000 (Sharma, Wu & Dalal, 2005) for every variation against its Oklab-rendered perceptual reference
- Tone curves (`linear`, `gamma`, `sigmoid`, `spline`) that reshape how source tones move through the duotone (`--curve`)
- Command-line mode with reproducible batches (`--seed`), configurable variation count (`--count`), explicit color groups of two to four colors (`--colors`), blend space (`--space srgb|linear|oklab`), and tone curve (`--curve`)
- Uses OpenCV for efficient image processing

## Example batch

One source photograph, 100 duotone variations generated with `--count 100 --seed 42 --space oklab`:

![Ten by ten grid of one hundred duotone variations of an astronaut portrait](https://raw.githubusercontent.com/MichailSemoglou/duotonics/main/docs/images/duotonics-contact-sheet.png)

Source: NASA's photograph of astronaut Eileen Collins, public domain, from the [scikit-image `astronaut` dataset](https://scikit-image.org/docs/stable/api/skimage.data.html#skimage.data.astronaut).

## Installation

Duotonics requires Python 3.9 or higher. Install it from PyPI:

```bash
python -m pip install duotonics
```

Or install from a source checkout:

```bash
git clone https://github.com/MichailSemoglou/duotonics.git
cd duotonics
python -m pip install .
```

Either way, this installs the `duotonics` command and the `duotone_generator` Python module.

## Usage

### Interactive mode

Run the command without arguments:

```bash
duotonics
```

Direct script execution remains available from a source checkout:

```bash
python3 duotone_generator.py
```

1. A file dialog will open. Select the image you want to process, then choose an output directory.

2. The script will generate 100 duotone variations in a new folder named `[original_filename]_duotone_variations`. Each variation is a lossless PNG file named after its hex color values (e.g. `083_2CD2B4_DC1E5A.png`). A `colors.csv` file is also written to the same folder, listing every variation's anchor colors and tone curve.

3. A message box will appear when the process is complete, indicating the location of the output folder.

### Command-line mode

Pass the image path as an argument to run without any dialogs, suitable for scripting and headless environments:

```bash
duotonics path/to/image.png [--output-dir DIR] [--count N] [--seed S] [--colors "HEX1,HEX2[,HEX3[,HEX4]]"] [--space {srgb,linear,oklab}] [--curve {linear,gamma,sigmoid,spline}]
```

- `-o, --output-dir DIR`: place the output folder under DIR instead of alongside the source image.
- `--count N`: number of random variations (default: 100).
- `--seed S`: reproduce a batch exactly by reusing its seed.
- `--colors "#2CD2B4,#DC1E5A"`: apply explicit color specifications instead of random ones; each holds two to four colors in tonal order (a tritone example: `--colors "#000000,#808080,#FFFFFF"`). Repeatable; one variation per specification. Cannot be combined with `--count` or `--seed`.
- `--space {srgb,linear,oklab}`: where the blend runs. `srgb` (default) is the legacy gamma-space blend. `linear` blends linear light (IEC 61966-2-1; Poynton, 2003) and `oklab` blends in Oklab (Ottosson, 2020); both derive the key from ITU-R BT.709 relative luminance instead of grayscale luma.
- `--curve {linear,gamma,sigmoid,spline}`: tone curve applied to the normalized tonal key before blending (default: `linear`, the identity). The selected curve is recorded in the `curve` column of `colors.csv`.

The location of the output folder is printed on completion.

### Tone curves

A tone curve remaps the source's normalized tonal key before the two colors are blended, so it controls which source tones land near the shadow color, the midpoint, and the highlight color. Every curve is a closed, named family with fixed parameters, keeps exact black and white endpoints, and serializes by name alone, so a recorded `curve` value always reproduces the same mapping.

![Four grayscale ramps showing the linear, gamma, sigmoid, and spline tone curves](https://raw.githubusercontent.com/MichailSemoglou/duotonics/main/docs/images/tone-curves.png)

- `linear`: the identity ramp. Source tones pass through unchanged; this is the default and matches earlier releases byte for byte.
- `gamma` (`x^2.2`, the classic display gamma of Poynton, 2003): darkens midtones toward the shadow color, so highlights stay clean while shadows gain depth.
- `sigmoid` (a renormalized logistic S-curve): steepest at the midpoint, so it raises midtone contrast while compressing both ends, protecting clipped highlights and crushed shadows.
- `spline` (the cubic smoothstep `3x^2 - 2x^3` of Perlin, 1985): a gentle S-curve with zero slope at both endpoints; it adds contrast around the midpoint without moving pure black or white.

Curves compose with any blend space and are independent of the color pair: `duotonics photo.png --space oklab --curve sigmoid --seed 42` blends in Oklab over a sigmoid-remapped key.

### Tritone and quadtone

With three or four anchor colors, the anchors sit at equally spaced tonal positions in [0, 1] (for a tritone, 0, 0.5, and 1) and each channel is interpolated with `np.interp` in the selected blend space, so the endpoints stay exact and the middle anchor lands exactly on a mid-key. Tone curves apply to the key first and compose unchanged with any anchor count: `duotonics photo.png --colors "#000000,#808080,#FFFFFF" --curve gamma`. In `colors.csv` and `metrics.csv`, the `color_3_hex` and `color_4_hex` columns appear only when a batch uses three or four anchors; pure two-color batches keep their two color columns and, on the default `linear` curve, byte-identical output (the only schema addition is the new `curve` column), and sidecars written by earlier releases remain readable.

Example batches of 100 tritone and quadtone variations (seed 42, `--space oklab`, same astronaut source as the example batch above):

![Ten by ten grid of one hundred tritone variations of an astronaut portrait, each tile labeled with its variation number](https://raw.githubusercontent.com/MichailSemoglou/duotonics/main/docs/images/duotonic-contact-sheet-tritone.png)

![Ten by ten grid of one hundred quadtone variations of an astronaut portrait, each tile labeled with its variation number](https://raw.githubusercontent.com/MichailSemoglou/duotonics/main/docs/images/duotonic-contact-sheet-quadtone.png)

### Library use

The module can be imported and used as a library, without tkinter installed:

```python
from duotone_generator import convert_to_duotone

folder, records = convert_to_duotone("photo.png", count=25, seed=42, space="oklab", curve="sigmoid")
```

It returns the output folder path and a list of `[index, color_1_hex, ..., color_N_hex]` records (two to four hex colors per variation), one per record. Each batch also writes `metrics.csv`, whose rows report the mean, median, and 95th-percentile CIEDE2000 (Sharma, Wu & Dalal, 2005) between that variation and its perceptual reference, defined as the same anchors blended in Oklab over the same key. See the `convert_to_duotone` docstring for all parameters, including `colors`, `space`, and `curve`.

## Dependencies

- OpenCV (cv2)
- NumPy
- Tkinter (usually comes pre-installed with Python; only needed for interactive mode)

## Requirements

- Python 3.9 or higher

## Development

Install the package in editable mode with its development dependencies, then run the test suite with coverage:

```bash
python -m pip install -e . -r requirements-dev.txt
pytest --cov --cov-report=term-missing
```

Build and check the wheel and source distribution with:

```bash
python -m build
python -m twine check dist/*
```

## Community

- [Contribution guide](https://github.com/MichailSemoglou/duotonics/blob/main/CONTRIBUTING.md)
- [Code of conduct](https://github.com/MichailSemoglou/duotonics/blob/main/CODE_OF_CONDUCT.md)
- [Security policy](https://github.com/MichailSemoglou/duotonics/blob/main/SECURITY.md)

## License

MIT. See the [LICENSE](https://github.com/MichailSemoglou/duotonics/blob/main/LICENSE) file for details.
