Tone and contrast
Tone is how the brightness of a picture is spread between black and white. A picture that is too dark, too flat or too bright can be fixed by moving its levels around. Every call here returns a new image, works on 1 to 4 channels, and never changes an alpha channel.
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
camera = stb.Image.open("camera.png")
def mean(img):
"""The mean value of all the channels, as a whole number."""
data = img.tobytes()
return sum(data) // len(data)
print("original:", mean(camera))
# Each call returns a new image. Brightness only changes in the default "luma" mode, so the colors of a photo do not shift.
results = {
"equalize": stb.equalize(camera), # every level used about as often
"auto_contrast": stb.auto_contrast(camera, 2.0), # drop the darkest and brightest 2 percent, stretch the rest
"clahe": stb.clahe(camera, 3.0, 8), # equalize each of 8 x 8 tiles, with the histogram cut at 3 times the mean
"gamma": stb.gamma(camera, 1.8), # above 1 brightens the middle
"levels": stb.levels(camera, 0.15, 0.85), # 0.15 becomes black and 0.85 becomes white
}
for name, result in results.items():
result.write(f"out/{name}.png")
print(f"{name}: {mean(result)}")
# Give one picture the tones of another: the histogram of the astronaut follows the one of the cat.
astronaut = stb.Image.open("astronaut.png")
chelsea = stb.Image.open("chelsea.png")
matched = stb.match_histogram(astronaut, chelsea)
matched.write("out/matched.png")
print(f"match_histogram: {mean(astronaut)} -> {mean(matched)}, the cat has {mean(chelsea)}")
Output
original: 129
equalize: 128
auto_contrast: 146
clahe: 133
gamma: 163
levels: 135
match_histogram: 114 -> 121, the cat has 115






What each one does
- equalize spreads the levels so that each is used about as often. It maximizes contrast and can look harsh.
- auto_contrast(cutoff) drops the darkest and brightest
cutoffpercent of the pixels and stretches what is left to the whole range, like Pillow'sImageOps.autocontrast. - clahe(clip_limit, tiles_x, tiles_y) is contrast-limited adaptive equalization: every tile is equalized on its own, with its histogram cut at
clip_limittimes the mean count of a bin (2 to 4 is usual), and each pixel mixes the mappings of its four nearest tiles. It brings out local detail without the harshness of a global equalize. - gamma(g) brightens the middle tones for a value above 1 and darkens them below 1, and leaves black and white where they are.
- levels(in_black, in_white, gamma, out_black, out_white) is the Levels dialog of an image editor: the range from
in_blacktoin_whiteis stretched to the full range, raised to the power1 / gamma(agammaabove 1 brightens the middle) and put betweenout_blackandout_white. All the levels are fractions of the full range. - match_histogram(reference) moves the levels of one picture so that its histogram follows the histogram of another, of any size with the same channels. The astronaut above takes on the tones of the cat.
Luma or per channel
A color picture keeps its colors when only the brightness changes. The default mode, luma, works on the brightness alone, so the hues do not shift. per_channel treats red, green and blue separately, which can change the colors. The mode is a parameter of equalize, auto_contrast, clahe and match_histogram.
The histogram
histogram counts how many pixels have each level. It returns a NumPy array, so it needs pip install "libstb[numpy]".
API at a glance
| Equalize | stb.equalize(img, mode="luma") |
|---|---|
| Auto contrast | stb.auto_contrast(img, cutoff, mode) |
| CLAHE | stb.clahe(img, clip_limit, tiles_x, tiles_y) |
| Gamma | stb.gamma(img, g) |
| Levels | stb.levels(img, in_black, in_white, g, out_black, out_white) |
| Match a histogram | stb.match_histogram(img, reference) |
| Count levels | stb.histogram(img) |