Fred's ImageMagick Scripts



    Licensing:

    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: http://www.imagemagick.org/script/license.php

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

COLOR2GRAY


Mixes the color channels from an image into a single grayscale image using a weighted combination

Download Script

last modified: December 15, 2018



USAGE: color2gray [-r red] [-g green] [-b blue] [-f form] [-c clrspace]
[-B bright] [-C contrast] infile outfile

USAGE: color2gray [-h or -help]

-r ... red ......... red mix percent; float; default=29.9
-g ... green ....... green mix percent; float; default=58.7
-b ... blue ........ blue mix percent; float; default=11.4
-f ... form ........ form of combining channels; add or rms
.................... (root mean squared channel combination) or colorspace
.................... intensity channel; default=add
-c ... clrspace .... colorspace in which to extract intensity channel;
.................... rec709luma, rec601luma (or HCL), OHTA (or HSI), sGray,
.................... LAB, HSL, BCH, HSB; default=HSL
-B ... bright ...... percent brightness change of grayscale image;
.................... -100<=float<=100; default=0
-C ... contrast .... percent contrast change of grayscale image;
.................... -100<=float<=100; default=0

PURPOSE: To mix the color channels from an image into a single grayscale image using a weighted combination.

DESCRIPTION: COLOR2GRAY mixes the color channels from an image into a single grayscale image using a weighted combination. The weighted channels can be combined by simple addition or by root mean squared combination. The image may also be converted to grayscale from the intensity-like channel of various colorspaces.

Arguments:

-r red ... RED is the red mix percent (weight) in forming the grayscale image. Values are (pos. or neg.) floats. The default=29.9 (equivalent to IM non-linear grayscale. Nominally the r,g,b,o weights should total 100%. Weights are not relevant to form=colorspace.

-g green ... GREEN is the red mix percent (weight) in forming the grayscale image. Values are (pos. or neg.) floats. The default=58.7 (equivalent to IM non-linear grayscale. Nominally the r,g,b,o weights should total 100%. Weights are not relevant to form=colorspace.

-b blue ... BLUE is the red mix percent (weight) in forming the grayscale image. Values are (pos. or neg.) floats. The default=11.4 (equivalent to IM non-linear grayscale. Nominally the r,g,b,o weights should total 100%. Weights are not relevant to form=colorspace.

-f form ... FORM of combining channels. The choices are: add (a) for a simple channel addition or rms (r) for a root mean squared channel combination or colorspace (c) for intensity-like channel of specified colorspace. The default=add.

-c clrspace ... CLRSPACE is the colorspace in which to get the intensity-like channel. The choices are: rec709luma, rec601luma (or HCL), OHTA (or HSI), sGray, LAB, HSL, BCH, HSB. The default=HSL. Note that rec709luma is the current -colorspace gray, while rec601luma is the older -colorspace gray before about IM 6.7.6.7. OHTA or HSI produce results the same as an equal weighted add. Also, sGray is a perceptual gray that tries to maintain the overall brightness of the color image. The colorspaces have been listed approximately in order of darkest to lightest results, though ordering will vary from image to image. Colorspace is not relevant to form=add or to form=rms.

-B bright ... BRIGHT is the percent change in brightness of the grayscale. image. Values are floats in the range of -100 to 100. The default=0.

-C contrast ... CONTRAST is the percent change in contrast of the grayscale. image. Values are floats in the range of -100 to 100. The default=0.

REQUIREMENTS: IM 6.7.9.0 or higher when using HCL or HSI colorspaces. IM 6.6.0.4 for BCH due to the use of -evaluate-sequence. IM 6.8.3.10 for sGray due to the use of -intensity.

REFERENCES:
http://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
http://www.kweii.com/site/color_theory/2007_LV/BrightnessCalculation.pdf

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.


EXAMPLES

Example 1

Original Image

Arguments:
-f add
(default r,g,b weights)

Arguments:
-f add -r 33.33 -g 33.33 -b 33.33

Arguments:
-f rms
(default r,g,b weights)

Arguments:
-f rms -r 33.33 -g 33.33 -b 33.33

Arguments:
-f colorspace -c rec709luma

Arguments:
-f colorspace -c rec601luma
(same as HCL)

Arguments:
-f colorspace -c ohta
(same as HSI or
form=add equal weights)

-f colorspace -c sgray

Arguments:
-f colorspace -c lab

Arguments:
-f colorspace -c hsl

Arguments:
-f colorspace -c bch

-f colorspace -c hsb

Animation of Colorspace Examples Above



Example 2

Original
(each square has same color brightness)
(source)

 

Arguments
-f c -c rec709luma

 

Arguments
-f c -c rec601luma

 

Arguments
-f c -c ohta
(same as equal weight average)

 

Arguments
-f c -c sgray

 

Arguments
-f c -c lab

 

Arguments
-f c -c hsl

 

Arguments
-f c -c bch

 

Arguments
-f c -c hsb

 

What the script does is as follows:

  • Use -color-matrix to mix channels into grayscale

This is equivalent to the following IM commands.

  • FOR FORM=ADD:
  • rf=`convert xc: -format "%[fx:$red/100]" info:`
  • gf=`convert xc: -format "%[fx:$green/100]" info:`
  • bf=`convert xc: -format "%[fx:$blue/100]" info:`
  • convert $infile -color-matrix "\
    $rf $gf $bf \
    $rf $gf $bf \
    $rf $gf $bf \
    " $outfile

  • FOR FORM=RMS:
  • rf=`convert xc: -format "%[fx:$red/100]" info:`
  • gf=`convert xc: -format "%[fx:$green/100]" info:`
  • bf=`convert xc: -format "%[fx:$blue/100]" info:`
  • convert $infile $setcspace -separate +channel \
    \( -clone 0 -clone 0 -compose multiply -composite -evaluate multiply $rf \) \
    \( -clone 1 -clone 1 -compose multiply -composite -evaluate multiply $gf \) \
    \( -clone 2 -clone 2 -compose multiply -composite -evaluate multiply $bf \) \
    -delete 0-2 -evaluate-sequence add -evaluate pow 0.5 $outfile

  • FOR FORM=COLORSPACE (with some variations for channel):
  • convert $infile -colorspace $clrspace -channel <b or r> -separate +channel $outfile