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.

CLIP


Stretches the channels of an image to full black and white according to a clip amount on each end of the histogram.

Download Script

last modified: December 15, 2018



USAGE: clip [-c colormode] [-l cliplow] [-h cliphigh] infile outfile
USAGE: clip [-help]

-c .... colormode ....... colorspace/channels in which to stretch/clip
......................... choices are: i, rgb, sb and sl; default=i
-l .... cliplow ......... clip amount on low end of histogram;
......................... count or percent; default is 0.1%
......................... if percent then float; 0<=cliplow<=100
......................... if count then integer; 0<=cliplow<=quantumrange
-l .... cliphigh ........ clip amount on high end of histogram;
......................... count or percent; default is 0.1%
......................... if percent then float; 0<=cliphigh<=100
......................... if count then integer; 0<=cliphigh<=quantumrange

PURPOSE: To stretch the channels of an image to full black and white according to a clip amount on each end of the histogram.

DESCRIPTION: CLIP stretches the channels of an image to full black and white according to a clip amount on each end of the histogram. The clip amount can be expressed in histogram counts or percent counts. Then the corresponding graylevels are used for the stretch. If the clip amount on both ends is zero, then the stretch will be performed from the channel minimum and maximum graylevel. The clip/stretch operation can be performed on various channel combinations. The choices are: intensity, red/green/blue, saturation/brightness and saturation/lightness.

ARGUMENTS:

-c colormode ... COLORMODE is the colorspace/channels in which to perform the clip/stretch. The choices are: i (for intensity), rgb, sb (for saturation/brightness) and sl (for saturation/lightness). The stretch will be performed on each channel independently. The default=i.

-l cliplow ... CLIPLOW is the count or percent at the low end of the histogram whose corresponding graylevel will be stretch to full black. Values for percent are floats between 0% and 100%. Values for count are integers between 0 and quantumrange for your Q level (e.g. 255 for Q8 and 65535 for Q16). NOTE: counts may only be used for IM 6.4.4-9 or higher. If cliplow=cliphigh=0 or 0%, then the stretch will locate the minimum value in the channel histogram. The default=0.1%

-l cliphigh ... CLIPHIGH is the count or percent at the high end of the histogram whose corresponding graylevel will be stretch to full white. Values for percent are floats between 0% and 100%. Values for count are integers between 0 and quantumrange for your Q level (e.g. 255 for Q8 and 65535 for Q16). NOTE: counts may only be used for IM 6.4.4-9 or higher. If cliplow=cliphigh=0 or 0%, then the stretch will locate the minimum value in the channel histogram. The default=0.1%

NOTE: The use of counts is limiting. For example, a 400x250 pixel image contains 100,000 pixels. The maximum clip on a Q8 system would be 255 counts. In percent, this is equivalent to 0.255%. Similarly the maximum clip on a Q16 system would be 65535 counts. In percent, this is equivalent to 65.535% which is much more reasonable. However, for a 1000x1000 image containing 1,000,000 pixels, the maximum clip in percent becomes only 6.5535%.

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


Variations In Colormode And Clip Amounts

Original Image

Arguments:
-c i -l 0 -h 0

Arguments:
-c sl -l 0 -h 0

Arguments:
-c sb -l 0 -h 0

Arguments:
-c rgb -l 0 -h 0

Original Image

Arguments:
-c i -l 0.1% -h 0.1%

Arguments:
-c sl -l 0.1% -h 0.1%

Arguments:
-c sb -l 0.1% -h 0.1%

Arguments:
-c rgb -l 0.1% -h 0.1%

Original Image

Arguments:
-c i -l 1% -h 1%

Arguments:
-c sl -l 1% -h 1%

Arguments:
-c sb -l 1% -h 1%

Arguments:
-c rgb -l 1% -h 1%



Variations In Colormode And Clip Amounts
(Image obtained from http://www.webdesign.org/web/photoshop/photoshop-basics/photo-correction-101-auto-color.8149.html)

Original Image

Arguments:
-c i -l 0 -h 0

Arguments:
-c sl -l 0 -h 0

Arguments:
-c sb -l 0 -h 0

Arguments:
-c rgb -l 0 -h 0

Original Image

Arguments:
-c i -l 0.1% -h 0.1%

Arguments:
-c sl -l 0.1% -h 0.1%

Arguments:
-c sb -l 0.1% -h 0.1%

Arguments:
-c rgb -l 0.1% -h 0.1%

Original Image

Arguments:
-c i -l 1% -h 1%

Arguments:
-c sl -l 1% -h 1%

Arguments:
-c sb -l 1% -h 1%

Arguments:
-c rgb -l 1% -h 1%



Variations In Colormode And Clip Amounts

Original Image

Arguments:
-c i -l 0 -h 0

Arguments:
-c sl -l 0 -h 0

Arguments:
-c sb -l 0 -h 0

Arguments:
-c rgb -l 0 -h 0

Original Image

Arguments:
-c i -l 0.1% -h 0.1%

Arguments:
-c sl -l 0.1% -h 0.1%

Arguments:
-c sb -l 0.1% -h 0.1%

Arguments:
-c rgb -l 0.1% -h 0.1%

Original Image

Arguments:
-c i -l 1% -h 1%

Arguments:
-c sl -l 1% -h 1%

Arguments:
-c sb -l 1% -h 1%

Arguments:
-c rgb -l 1% -h 1%



What the script does is as follows:

  • Extracts the I or R,G,B or H,S,L or H,S,B channels of the image
    according to the user supplied colormode argument
  • If cliplow=cliphigh=0, it finds the min and max values of the channel(s) and
    uses those values with -level to stretch the channel(s)
  • If cliplow and cliphigh are not both zero, then it uses the supplied
    values (count or percent count depending whether the % symbol is included)
    with -contrast-stretch to stretch the channel(s)
  • For colormode I, the image channels are stretched in concert
  • For colormode SL or SB, the S,L or S,B channels are stretched independently
    and the H channel is not changed
  • For colormode RGB, the image channels are stretched independently
  • All the channels for the appropriate colorspace are then combined again.

This is equivalent to the following IM commands for colormode=sl and cliplow=cliphigh=0.1%

  • convert $infile -colorspace HSL -channel R -separate $tmpH1
  • convert $infile -colorspace HSL -channel G -separate $tmpS1
  • convert $infile -colorspace HSL -channel B -separate $tmpL1
  • if [ "$im_version" -lt "06040409" ]; then
  • cliphighnum=`convert xc: -format "%[fx:100-$cliphighnum]" info:`
  • fi
  • convert $tmpS1 -contrast-stretch ${cliplownum}%,${cliphighnum}% $tmpS1
  • convert $tmpL1 -contrast-stretch ${cliplownum}%,${cliphighnum}% $tmpL1
  • convert $tmpH1 -colorspace HSL \
    $tmpH1 -compose CopyRed -composite \
    $tmpS1 -compose CopyGreen -composite \
    $tmpL1 -compose CopyBlue -composite \
    -colorspace RGB $outfile