Skip to content
docs
GitHub 4.5M /week v2.2.0
Color

normalize()

Stretch each channel to use the full 0 to 255 range.

From @jimp/plugin-color

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

normalize.d.ts
image.normalize(): this

normalize takes no arguments. It changes the image in place and returns it, so you can chain more calls after it.

Example

example.ts
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:

example.ts
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.

Try normalize()
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
PNG
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, normalize finds the lowest value min and the highest value max used anywhere in the image, then maps each value c to (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 greyscale first or use contrast.
  • 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 normalize do 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 min and max. 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.