normalize stretches the levels of a dull or hazy image so that its darkest value becomes 0 and its brightest becomes 255.
It works on each channel separately, so it can also correct a slight color cast.
Signature
image.normalize(): thisnormalize takes no arguments. It changes the image in place and returns it, so you can chain more calls after it.
Example
import { Jimp } from "jimp";
const image = await Jimp.read("scan.jpg");
image.normalize();
await image.write("scan-normalized.jpg");Clean up a grayscale scan
For documents, remove the color first so the three channels stretch together:
image.greyscale().normalize();Try it
Run the playground to see the result. Hold the compare button to see the original. Upload a low-contrast photo to see the biggest difference.
image.normalize()Stretch each channel so its values use the full 0 to 255 range.
This method has no options.
Loading Jimp…Drop an image to load it
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- How it’s computed. For each channel,
normalizefinds the lowest valueminand the highest valuemaxused anywhere in the image, then maps each valuecto(c − min) × 255 / (max − min). Fractions are dropped when the value is stored. - Per channel. Red, green and blue are stretched independently, which can shift the colors. If you want only the brightness stretched, call
greyscalefirst or usecontrast. - Flat channels are left alone. If a channel has the same value in every pixel, there’s no range to stretch and that channel isn’t changed. A solid-color image stays the same.
- Outliers limit the effect. A single pure black and a single pure white pixel are enough to make
normalizedo nothing. There’s no option to ignore a percentage of extreme pixels. - Transparent pixels count. The color stored in fully transparent pixels is included when finding
minandmax. Alpha itself is unchanged. - Changed in 2.0: a channel with a single value is left unchanged; before, it was set to
0, so a solid-color image turned black.