How to Normalize Image Colors with ImageMagick?
The ImageMagick convert command allows users to
normalize the color channels of an image, expanding the contrast so that
the darkest pixels become black and the lightest pixels become white.
This process spans the full dynamic range of the image, correcting
under-exposed or faded photos by distributing the color channels more
evenly. This article provides a quick overview of how the
-normalize operator works, provides standard command-line
examples, and explains how to target individual color channels for
precise adjustments.
Understanding the Normalize Operator
When you apply the -normalize flag in ImageMagick, the
software analyzes the histogram of the image. It automatically stretches
the contrast, but with a built-in safety feature to prevent extreme
pixel clipping. By default, ImageMagick clips 2% of the darkest pixels
to pure black and 1% of the brightest pixels to pure white.
This helps eliminate noise at the extremes of the spectrum while ensuring the overall image gains clarity and vibrancy.
Basic Syntax and Examples
The most direct way to normalize an image is to apply the global
-normalize operator. This adjusts all color channels (Red,
Green, and Blue) simultaneously based on the overall luminance of the
image.
convert input.jpg -normalize output.jpgIf you are using ImageMagick v7 or newer, the magick
command is preferred over convert, though the syntax
remains identical:
magick input.jpg -normalize output.jpgNormalizing Individual Color Channels
Sometimes, a global normalization can introduce an unwanted color
cast if one channel is significantly more distorted than the others. To
solve this, you can instruct ImageMagick to isolate and normalize
specific channels independently using the -channel
flag.
For example, to normalize only the Red and Green channels while leaving the Blue channel untouched, you would use the following command:
convert input.jpg -channel RG -normalize output.jpgCustomizing Contrast Stretching
If the default 2% and 1% clipping thresholds of the
-normalize command do not yield the desired results, you
can use the closely related -contrast-stretch operator.
This command gives you exact control over how much of the black and
white points are clipped.
The syntax requires you to specify the black-point and white-point clipping values as percentages:
convert input.jpg -contrast-stretch 1%x1% output.jpgIn this example, exactly 1% of the darkest pixels are pushed to black, and 1% of the brightest pixels are pushed to white, offering a more conservative contrast enhancement than the standard normalization command.