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. |
Mixes the color channels from an image into a single grayscale image using a weighted combination |
last modified: December 15, 2018
USAGE: color2gray [-r red] [-g green] [-b blue] [-f form] [-c clrspace]
-r ... red ......... red mix percent; float; default=29.9 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: 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. |
Example 1 | ||||
Original Image |
||||
![]() |
||||
Arguments: |
Arguments: |
Arguments: |
Arguments: |
|
![]() |
![]() |
![]() |
![]() |
|
Arguments: |
Arguments: |
Arguments: |
-f colorspace -c sgray |
|
![]() |
![]() |
![]() |
![]() |
|
Arguments: |
Arguments: |
Arguments: |
-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:
This is equivalent to the following IM commands.
|