Fred's ImageMagick Scripts


    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at:

    Please read the Pointers For Use on my home page to properly install and customize my scripts.


Generates an image that keeps only the channel with the maximum value.

Download Script

last modified: December 15, 2018

USAGE: maxrgb infile outfile
USAGE: maxrgb [-h|-help]

There are no options.

PURPOSE: To generate an image that keeps only the channel with the maximum value.

DESCRIPTION: MAXRGB generate a new image that keeps the maximum value of each of the RGB channels of an RGB image and zeroes the other two channels. The mathematics are:
For each pixel in the input image:
Get the r, g and b pixel values.
Determine the maximum value from r, g and b and save as m
If r < m, then set r = 0
If g < m, then set g = 0
If b < m, then set b = 0
Store the new r, g, and b values into the output image at that pixel location.

REQUIREMENTS: Input must be RGB colorspace.


CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


Original Image


What the script does is as follows:

  • Separates the R,G,B channels as new images
  • Computes a new image that is the maximum value of each of the new R,G,B images
  • Does compose minus compare of each R,G,B image with the maximum image and thresholds to create a mask
  • Applies a compose multiply between each R,G,B image and its corresponding mask image
  • Combines the masked R,G,B images back into an RGB image

This is equivalent to the following IM commands:

  • convert -quiet "$infile" $dir/tmpI.mpc
  • convert $dir/tmpI.mpc -separate +channel +write $dir/tmpS.mpc -evaluate-sequence max $dir/tmpM.mpc
  • convert \( $dir/tmpS.mpc[0] -evaluate add 1 \) $dir/tmpM.mpc +swap -compose minus -composite -threshold 0 $dir/tmpMR.mpc
  • convert \( $dir/tmpS.mpc[1] -evaluate add 1 \) $dir/tmpM.mpc +swap -compose minus -composite -threshold 0 $dir/tmpMG.mpc
  • convert \( $dir/tmpS.mpc[2] -evaluate add 1 \) $dir/tmpM.mpc +swap -compose minus -composite -threshold 0 $dir/tmpMB.mpc
  • convert \
    \( $dir/tmpI.mpc[0] $dir/tmpMR.mpc -compose multiply -composite \) \
    \( $dir/tmpI.mpc[1] $dir/tmpMG.mpc -compose multiply -composite \) \
    \( $dir/tmpI.mpc[2] $dir/tmpMB.mpc -compose multiply -composite \) \
    -set colorspace sRGB -combine -colorspace sRGB "$outfile"