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.

SHADOWS


Applies drop shadows to an image.

Download Script

last modified: December 15, 2018



USAGE: shadows [-t type] [-c color] [-r radius] [-s sigma] [-d direction] [-b bgcolor] [-a alpha] infile outfile
USAGE: shadows [-h or -help]

-t ... type ........ type of shadow; inner (i) or outer (o); default=inner
-c ... color ....... shadow color; any valid Imagemagick opaque color may be used;
.................... default=black.
-r ... radius ...... radius of shadow; integer>=0; default=0 (determined from
.................... sigma)
-s ... sigma ....... sigma (blur) of shadow; float>=0; default=5
-d ... direction ... direction of shadow; -360<=integer<=360; default=135
-b ... bcolor ...... color to put under the alpha channel; any valid Imagemagick
.................... opaque color may be used; default is not to replace the image
.................... texture with color.
-a ... alpha ....... flag to specify to leave alpha channel enabled; yes (y) or
.................... no (n); default=no; only allowed for type=inner

infile must have an alpha channel

PURPOSE: To apply drop shadows to an image.

DESCRIPTION: SHADOWS applies drop shadows to an image that has an alpha channel. The alpha channel should be binary or at worst binary but anti-aliased. If there is no alpha channel or the alpha channel is full opaque, then the no shadow will be created. The inner shadow will appear at the inside edges of the white part of the alpha channel. The outer shadow will appear at the outside edges of the white part of the alpha channel. In either case, the result will have the alpha channel removed to show the colors under the alpha channel. There is an option for the inner shadow to keep the alpha channel enabled. Note that the inner shadow may look like an outer bevel. This is an optical illusion.

ARGUMENTS:

-t type ... TYPE of shadow. The choices are: inner (i) or outer (o). The default=inner.

-c color ... COLOR of shadow; any valid Imagemagick opaque color may be used. The default=black.

-r radius ... RADIUS of the shadow. Values are integer>=0. The default=0. The actual value will be deterined from the sigma value.

-s sigma ... SIGMA (blur) of the shadow. Values are float>=0. The default=5.

-d direction ... DIRECTION of the shadow. Values are -360<=integer<=360 measure clockwise from the positive x axis. The default=135.

-b bcolor ... (background) BCOLOR to put under the alpha channel. Any valid Imagemagick opaque color may be used. The default is not to replace the image texture with color.

-a alpha ... ALPHA flag to specify whether to leave the alpha channel enabled or not. Values are on or off. The default=off. This option is only allowed for type=inner.

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


Examples

Original
convert -size 600x -background black -fill white \
-font ubuntu-bold -gravity center label:TESTING \
-alpha off -alpha copy -background skyblue -alpha shape \
-background white -alpha background testing.png

Original Without Alpha Channel

Original Alpha Channel

Arguments:
-t inner -c black -s 5 -d 135 -a on

Arguments:
-t inner -c black -s 5 -d 135 -a off

Arguments:
-t outer -c black -s 5 -d 135

Arguments:
-t outer -c "gray(50)" -s 5 -d 135

Arguments:
-t outer -c "black" -s 5 -d 135 -b pink



What the script does is as follows for border=black at the top/bottom edges:

  • read image
  • clone and turn off alpha
  • clone and extract alpha
  • delete original
  • clone alpha, colorize, a apply motion blur
  • composite multiply: image (without alpha), shadow, mask
  • write output

This is equivalent to the following IM commands

  • if [ "$type" = "outer" ]; then
    negating="-negate"
    else
    negating=""
    fi
  • angle=$((direction-180))
  • convert "$infile" \
    \( -clone 0 -alpha off \) \
    \( -clone 0 -alpha extract $negating \) \
    -delete 0 \
    \( -clone 1 \( +clone -fill "$color" -colorize 100 \) \
    +swap +duplicate -compose over -composite \
    -motion-blur ${radius}x${sigma}+${angle} \) \
    -swap 1,2 \
    -compose multiply -composite \
    "$outfile"