Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial 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

LEVELCOLORS


Applies a color level transformation to an image channel-by-channel.

Download Script

last modified: April 25, 2015



USAGE: levelcolors [-c channels] [lowcolorxhighcolor] infile outfile
USAGE: levelcolors [-h or -help]

-c .... channels .......... channels are the sequence of channels to be
........................... processed. Choices are: any combinations
........................... of 1 to 4 of r,g,b,a such rgb or rgba;
........................... The default is all available channels
........................... in the image. IMPORTANT: if specified,
........................... then it MUST be before lowcolorxhighcolor
........................... in the command line!
lowcolorxhighcolor ........ lowcolor is the color values to map to black;
........................... highcolor is the color values to map to white;
........................... the default is "rgba(0%,0%,0%,0)xrgba(100%,100%,100%,0)"
........................... namely, (opaque) blackxwhite.

infile is limited to single channel gray or three channel rgb with or
without an alpha channel. CMYK is excluded.

PURPOSE: To apply a color level transformation to an image channel-by-channel.

DESCRIPTION: LEVELCOLORS apply a color level transformation to an image using the -level operator on a channel-by-channel basis using color values specified to be mapped to black and to white.

ARGUMENTS:

-c channels ... CHANNELS are the sequence of channels to be processed. Choices are: any combinations of 1 to 4 of r,g,b,a such rgb or rgba. For grayscale images with alpha, specify r if you do not want the alpha channel processed. Specify rgb if you want to colorize a grayscale image. The default is all available channels in the image. IMPORTANT: if channels are specified, then it must be before lowcolorxhighcolor in the command line.

lowcolorxhighcolor ... LOWCOLORxHIGHCOLOR are the two colors to be mapped to black and white respectively on a channel-by-channel basis. If no color values are provided, then the output will be the same as the input. If lowcolor is left off (value specified as xHIGHCOLOR), then low color of rgba(0%,0%,0%,0) (opaque black) will be used. If highcolor is left off (value specified either as LOWCOLOR or xLOWCOLOR), then highcolor will be set to the complement of the lowcolor, i.e. (QuantumRange minus lowcolor). Colors may be specified in an valid IM format: rgb(...), rgba(...), hexcolor values or color names. Use of color names assumes the name does not include the letter "x". If not using color names, you must enclose this argument in quotes. Alpha values may be included. Note: graya(...) color specifications do not seem to be working to generate any amount of transparency prior to IM 6.4.1-8. See http://imagemagick.org/script/color.php

NOTE: infile is limited to single channel gray or three channel rgb with or without an alpha channel. CMYK is excluded. The alpha channel will not be modified, but will be tranferred from input to output.

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


Color Transformations Of An Image

Original Image

No Change
Arguments:
blackxwhite
or
"gray0xgray100"
or
"rgb(0,0,0)xrgb(255,255,255)"
or
"rgb(0%,0%,0%)xrgb(100%,100%,100%)"

Original Image

Invert Colors
(equivalent to -negate)
Arguments:
whitexblack
or
"gray100xgray0"
or
"rgb(255,255,255)xrgb(0,0,0)"
or
"rgb(100%,100%,100%)xrgb(0%,0%,0%)"

Original Image

Invert Colors
(equivalent to -negate)
Arguments:
whitexblack
or
"gray100xgray0"
or
"rgb(100%,100%,100%)xrgb(0%,0%,0%)"

Original Image

Invert Red Channel Only
(equivalent to -channel R -negate)
Arguments:
-c r whitexblack
or
-c r "gray100xgray0"
or
-c r "rgb(100%,100%,100%)xrgb(0%,0%,0%)"

Original Image

Increase Contrast
20% on low and high end
Arguments:
"rgb(20%,20%,20%)xrgb(80%,80%,80%)"

Original Image

Increase Brightness
20% on high end
Arguments:
"rgb(0%,0%,0%)xrgb(80%,80%,80%)"

Original Image

Decrease Brightness
20% on low end
Arguments:
"rgb(20%,20%,20%)xrgb(100%,100%,100%)"

Original Image

Increase Brightness Of Green Channel Only
20% on high end
Arguments:
"rgb(0%,0%,0%)xrgb(100%,80%,100%)"

Original Image
Gradient Alpha Channel
convert zelda3.jpg \
\( -size 128x128 gradient: \) \
+matte -compose copy_opacity \
-composite zelda3_agrad.png

Negate Alpha Channel
Arguments:
"rgba(0%,0%,0%,1)xrgba(100%,100%,100%,0)"
(note use of rgba and four color values)

Original Image

Threshold To Pure White
Arguments:
blackxblack
or
"gray0xgray0"
or
"rgb(0%,0%,0%)xrgb(0%,0%,0%)"
(note use of complementary colors or values)

Original Image

Threshold To Pure Black
Arguments:
whitexwhite
or
"gray100xgray100"
or
"rgb(100%,100%,100%)xrgb(100%,100%,100%)"
(note use of complementary colors or values)



Grayscale Colorization
(Note the use of -c rgb to enable rgb in a grayscale image)

Original Image

Red Gradient
Arguments:
cyanxwhite
or
-c rgb "rgb(0%,100%,100%)xrgb(100%,100%,100%)"
(note use of complementary colors or values)

Original Image

Blue To Red Gradient
Arguments:
cyanxyellow
or
-c rgb "rgb(0%,100%,100%)xrgb(100%,100%,0%)"
(note use of opposite complementary colors or values)



What the script does is as follows:

  • Extracts the low and high colors from the arguments and
    converts them to r,g,b,(a) values
  • Applies the low and high r,g,b,(a) values using -level
    on a channel-by-channel basis

This is equivalent to the following IM commands to negate a RGB image:
levelcolors whitexblack image.jpg image_neg.jpg

  • code to separate white and black from "whitexblack"
  • code to convert white to rlo,glo,blo=100% each and black to rhi,ghi,bhi=0% each
  • convert $infile -channel Red -separate $tmpRd
  • convert $infile -channel Red -separate $tmpGr
  • convert $infile -channel Red -separate $tmpBl
  • convert $tmpRd -level ${rlo}x${rhi} $tmpRd
  • convert $tmpGr -level ${glo}x${ghi} $tmpGr
  • convert $tmpBl -level ${blo}x${bhi} $tmpBl
  • convert $tmpRd $tmpGr $tmpBl -channel RGB -combine $outfile