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

COLOR2ALPHA


Creates a ramped alpha channel from the input image according the specified color.

Download Script

last modified: September 26, 2017



USAGE: color2alpha [-ca alphacolor] [-cr replacecolor] [-a] [-g gain] infile outfile
USAGE: color2alpha [-h or -help]

-ca ... alphacolor ..... color to to become transparent in the alpha channel;
........................ any valid opaque IM color; default=red
-cr ... replacecolor ... color to use to replace alphacolor in the output;
........................ any valid IM color; default does no color
........................ replacement in the output and the alpha channel
........................ is attached to the input.
-a ..................... output only the alpha channel.
-g .... gain ........... gain control for transiton between black and white
........................ in the alpha channel; float>0; default=1;
........................ values larger than 1 will show more transparency;
........................ values smaller than 1 will show less transparency;

PURPOSE: To create a ramped alpha channel from the input according the specified color.

DESCRIPTION: COLOR2ALPHA creates a ramped alpha channel from the input image according to the specified color. The output can be either the alpha mask alone or made as an alpha channel on the input image. If a second replacement color is provided, then the output will use the alpha channel to replace the specified colors and those near it with the replacement color. This is useful to change one color to another with anti-aliasing, especially when converting background color under colorized text or vice-versa.

ARGUMENTS:

-ca alphacolor ... ALPHACOLOR is the color that is to become transparent in the alpha channel. Any valid opaque IM color is allowed. The default=red.

-cr replacecolor ... REPLACECOLOR is the color to use to replace the alphacolor in the output. Any valid IM color is allowed. The default does no color replacement in the output and the alpha channel is attached to the input.

-a ... output only the ALPHA channel.

-g gain ... GAIN control for the transition between black and white in the alpha channel. Values are floats>0. The default=1. Values larger than 1 will show more transparency and a shorter transition. Value smaller than 1 will show less transparency and a longer transition.

REFERENCE: http://www.gimp.org/tutorials/Changing_Background_Color_1/

REQUIREMENTS: IM 6.6.0.4 in order to support -evaluate-sequence max

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 --- Rainbow Image; Alpha Only

Original

Arguments:
-ca red -g 0.5 -a

Arguments:
-ca red -g 1 -a

Arguments:
-ca red -g 2 -a



EXAMPLE 2 --- Rainbow Image; Alpha Channel On Input

Original

Arguments:
-ca red -g 0.5

Arguments:
-ca red -g 1

Arguments:
-ca red -g 2



EXAMPLE 3 --- Rainbow Image; Red Replaced With Cyan

Original

Arguments:
-ca red -g 0.5 -a

Arguments:
-ca red -g 1 -a

Arguments:
-ca red -g 2 -a



EXAMPLE 4 --- Text Background Blue Color Replace With Red

Original
(source)

Original 6x Z00M

Arguments:
-ca "#7449FF" -cr red

Arguments:
convert pr.png -fuzz 0% -fill "red" \
-opaque "#7449FF" pr_f0_blue2red.png

Arguments:
convert pr.png -fuzz 50% -fill "red" \
-opaque "#7449FF" pr_f0_blue2red.png



What the script does is as follows:

  • Clones the input image and fills with the specified alpha color
  • Performs a -compose different composition
  • Separates the channels and get the max value at each pixel
    from the R,G,B channels
  • Applies an auto-level
  • Puts the result into the alpha channel of the input image
  • Flattens the result against the replace color

This is equivalent to the following IM commands

  • convert -quiet "$infile" -alpha off $dir/tmp1.mpc
  • convert $dir/tmp1.mpc \
    \( -clone 0 -fill "$alphacolor" -colorize 100 \) \
    -compose difference -composite \
    -separate -evaluate-sequence max \
    -auto-level -evaluate pow $gain \
    $dir/tmp2.mpc
  • if [ "$alphaout" = "yes" ]; then
    convert -quiet $dir/tmp2.mpc "$outfile"
    elif [ "$color2" = "" ]; then
    convert -quiet $dir/tmp1.mpc $dir/tmp2.mpc \
    -alpha off -compose copy_opacity -composite "$outfile"
    elif [ "$color2" != "" ]; then
    convert -quiet $dir/tmp1.mpc $dir/tmp2.mpc \
    -alpha off -compose copy_opacity -composite \
    -compose over -background "$replacecolor" -flatten "$outfile"
    fi