Per-channel scanner gain for color negatives, from one scan of the film base (orange mask).
Find a file
2026-11-18 22:55:43 -05:00
tests film-base-gain: Per-channel scanner gain for color negatives, from one scan of the film base (orange mask). 2026-11-18 22:55:43 -05:00
.gitignore film-base-gain: Per-channel scanner gain for color negatives, from one scan of the film base (orange mask). 2026-11-18 22:55:43 -05:00
commons.toml film-base-gain: Per-channel scanner gain for color negatives, from one scan of the film base (orange mask). 2026-11-18 22:55:43 -05:00
film_base_gain.py film-base-gain: Per-channel scanner gain for color negatives, from one scan of the film base (orange mask). 2026-11-18 22:55:43 -05:00
LICENSE film-base-gain: Per-channel scanner gain for color negatives, from one scan of the film base (orange mask). 2026-11-18 22:55:43 -05:00
README.md film-base-gain: Per-channel scanner gain for color negatives, from one scan of the film base (orange mask). 2026-11-18 22:55:43 -05:00

film-base-gain

Works out per-channel scanner gain for color negatives from one scan of the film base, so the orange mask lands just under full scale and every bit of the scanner's range goes to the picture.

Problem

A color negative's orange mask makes blue and green come out dim: most of the scanner's 16-bit range in those channels sits unused, and the dark parts of the picture end up in the noisy bottom codes. Scanner drivers that support analog gain need a factor per channel. The film base (no exposure, just mask) is the brightest thing on the roll, so it sets the ceiling.

Usage

Scan the leader, or a clear gap between frames, with no extra gain, as a 16-bit RGB TIFF:

python -I film_base_gain.py BASE.tif                       # plain: "R G B"
python -I film_base_gain.py BASE.tif --format json
python -I film_base_gain.py BASE.tif --format env --env-name MY_DRIVER_GAIN

On a synthetic base (red 30000, green 18000, blue 9000, with grain):

R: base median  29999, 99.5%  30771 -> factor 1.81
G: base median  18000, 99.5%  18776 -> factor 2.97
B: base median   9000, 99.5%   9773 -> factor 5.70
1.81 2.97 5.70

The per-channel lines go to stderr; only the factors go to stdout, so you can capture them. From Python: film_base_gain.gain_factors(array, target=0.85, max_gain=8.0).

How it works

Takes the middle two thirds of the scan (away from holder edges), finds each channel's 99.5th percentile, and returns target * 65535 / p99.5, clipped to 1 through --max-gain. Using the 99.5th percentile rather than the median keeps the brightest base pixels, not the average ones, under the target. Warns when a channel varies by more than 25% (10th to 90th percentile over median), which usually means the scan caught exposed frame or holder.

Tested on

  • python -I tests/test_film_base_gain.py: synthetic bases; checks factors put the base at target, the holder border is ignored, the cap and the floor of 1, grayscale is rejected, the warning, and the output formats.
  • Real use: one roll of color negative on a dedicated 35 mm film scanner, feeding a driver's analog gain setting (2026-11-18).

Known limits

  • Assumes the base scan was made with no gain applied; factors multiply whatever gain was active.
  • The scanner applies the nearest gain it supports, so the result lands near, not exactly at, the target. Leave headroom (the default 0.85) for that and for density variation along the roll.
  • Not useful for slide film or black and white, which have no mask to balance.
  • Expects RGB, 16-bit full scale (pass full_scale to gain_factors for other depths).

Requirements

Python 3.10+, numpy, tifffile.