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.

SLICE


Thresholds an image (inclusively) between a range of graylevels and optionally colorizes inside and/or outside the threshold range.

Download Script

last modified: December 06, 2023



USAGE: slice blackpt,whitept [-m mode] [-i icolor] [-e ecolor] infile outfile
USAGE: slice [-h or -help]

blackpt,whitept ...... low and high thresholds; integers in range 0-255
...................... for mode=8, integers in range 0-65535 for mode=16
...................... and float in range 0-100 for mode=P (for percent)
-m .... mode ......... numerical mode for threshold values; values are:
...................... 8 for 8 bit range (0-255), 16 for 16 bit range
...................... (0-65535) if Q=16 or 32 or P for percent (0-100);
...................... default=8 for Q8; otherwise 16 for Q16 or Q32
-i .... icolor ....... color for range inclusive of blackpt to whitept;
...................... any valid IM non-transparent color, 'none' for
...................... transparency, or 'image' to leave image coloration
-e .... ecolor ....... color for range exclusive of blackpt to whitept
...................... any valid IM non-transparent color, 'none' for
...................... transparency, or 'image' to leave image coloration

PURPOSE: To threshold an image between a range of graylevels and optionally colorizes inside and/or outside the threshold range.

DESCRIPTION: SLICE thresholds an image (inclusively) between a range of graylevels and optionally colorizes inside and/or outside the threshold range. The inclusive range of graylevels may be set to any non-transparent color, transparent or left as image coloration. Similarly the exclusive range of color may be set to any non-transparent color, transparent or left as image coloration. If the input image has transparency, the transparent channel will be extracted and recombined with the output image. This will supercede any transparency used in the processing.

ARGUMENTS:

blackpt,whitept ... BLACKPT,WHITEPT are the low and high threshold values used to determine the inclusive range and corresponding coloration. Values are restricted to integers between 0 and 255 when mode=8, between 0 and 65535 when mode=16, and floats between 0 and 100 when mode=P (for percent).

-m mode ... MODE is the numerical mode for specifying the threshold values. Values are restricted to integers between 0 and 255 when mode=8, between 0 and 65535 when mode=16, but floats between 0 and 100 are allowed when mode=P (for percent). Default=8 for Q8; otherwise 16 for Q16 or Q32

-i icolor ... ICOLOR is the color to be used inclusive to the threshold range. Any valid IM non-transparent color specification is allowed. One may also use 'none' to specify fully transparent or 'image' to leave the image coloration. Default=white

-e ecolor ... ECOLOR is the color to be used exclusive to the threshold range. Any valid IM non-transparent color specification is allowed. One may also use 'none' to specify fully transparent or 'image' to leave the image coloration. Default=black

NOTE: If the input has transparency, the transparency channel will be removed, saved, the image processed without it and then combined back at the end. This will supercede the use of any transparency used in the processing.

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


Gradient

Arguments:
"85,170" -m 8 -i black -e white

Arguments:
"85,170" -m 8 -i black -e red

Arguments:
"85,170" -m 8 -i black -e none

Arguments:
"85,170" -m 8 -i black -e image

Arguments:
"85,170" -m 8 -i image -e black

Arguments:
"85,170" -m 8 -i image -e red

Arguments:
"85,170" -m 8 -i image -e none

Arguments:
"85,170" -m 8 -i image -e white

Arguments:
"85,170" -m 8 -i none -e black

Arguments:
"85,170" -m 8 -i none -e red

Arguments:
"85,170" -m 8 -i none -e image

Arguments:
"85,170" -m 8 -i none -e white

Arguments:
"85,170" -m 8 -i red -e image

Arguments:
"85,170" -m 8 -i red -e none

Arguments:
"85,170" -m 8 -i red -e white

Arguments:
"85,170" -m 8 -i white -e black

Arguments:
"85,170" -m 8 -i white -e image

Arguments:
"85,170" -m 8 -i white -e none



Lena

Arguments:
"45,55" -m P -i black -e white

Arguments:
"45,55" -m P -i black -e red

Arguments:
"45,55" -m P -i black -e none

Arguments:
"45,55" -m P -i black -e image

Arguments:
"45,55" -m P -i image -e black

Arguments:
"45,55" -m P -i image -e red

Arguments:
"45,55" -m P -i image -e none

Arguments:
"45,55" -m P -i image -e white

Arguments:
"45,55" -m P -i none -e black

Arguments:
"45,55" -m P -i none -e red

Arguments:
"45,55" -m P -i none -e image

Arguments:
"45,55" -m P -i none -e white

Arguments:
"45,55" -m P -i red -e black

Arguments:
"45,55" -m P -i red -e none

Arguments:
"45,55" -m P -i red -e image

Arguments:
"45,55" -m P -i red -e white

Arguments:
"45,55" -m P -i white -e black

Arguments:
"45,55" -m P -i white -e red

Arguments:
"45,55" -m P -i white -e none

Arguments:
"45,55" -m P -i white -e image



What the script does is as follows:

  • Creates a 1-row gradient of either 8 or 16 bits
  • Applies -white-threshold with blackpt
    to gradient and converts white to transparent
  • Applies -black-threshold with whitept
    to gradient and converts black to transparent
  • Composites the two results
  • Converts everything non-transparent to black
  • Converts everything transparent to white
  • Apply the modified gradient to the image
    as a lut using -clut
  • If icolor=image or ecolor=image, then overlay the result
    as an alpha channel to the image.
  • Colorizes white (inclusive) and black (exclusive)
    as desired

This is equivalent to the following IM commands
for the case of icolor=white and ecolor=image on
a Q16 IM for an image with no transparency channel.

  • convert -size 1x65536 gradient: -rotate 90 $tmpA2
  • convert $tmpA2 \
    \( -clone 0 -white-threshold $blackpt \
    -channel rgba -alpha on -fill none -opaque white \) \
    \( -clone 0 -black-threshold $whitept \
    -channel rgba -alpha on -fill none -opaque black \) \
    -delete 0 -compose over -composite \
    -fill black +opaque none \
    -fill white -opaque none \
    -alpha off \
    $tmpA2
  • convert \( $infile -colorspace gray \) \( $tmpA2 -negate \) \
    -clut $infile +swap -compose copy_opacity -composite \
    -channel rgba -alpha on -fill white -opaque none -alpha off \
    $outfile