Adding drop shadow with ImageMagick

Why use the command line?

Why not? You could make a script file and apply this to multiple files in one go. You get the exact settings you want. It’s free. It’s quicker than opening Photoshop or other image editors (if you have a script or button). Shall I go on?

The basics

The most basic form makes a shadow around the image, but it also moves the image from the center to make room for the shadow.

magick convert image.png ( -clone 0 -background black -shadow AxB+C+D ) -reverse -background none -layers merge +repage output.png

Best practice

The recommended best practice here preserves the images original location on the virtual canvas. This is done by adding a invisible border around the image so that the image stays on center no matter how big the shadow is.

magick convert input.png -bordercolor None -border 7x7 ( +clone -background black -shadow 70x2+1+1 ) -background none -compose DstOver -flatten -compose Over output.png

Example:

The long explanation

magick convert input.png starts the processing of your input image.

-bordercolor None -border 7x7 adds an invisible border to make room for the shadow.

clone will copy the existing image without making alterations.

-background black sets the color of the shadow. You could for example use -background "rgb(255, 130, 171)" to get that pink or navy.

-shadow AxB+C+D sets the properties of the shadow. Let’s go into the details here:

All these letters are values of integers or decimals.

A = The strength (or the opacity level) of the shadow.
B = The width of the shadow on all sides. Note that this has to be adjusted together with the border weight.
C = The horizontal degree of the drop shadow. + is right side – is left side.
D = The vertical degree of the drop shadow. + is bottom – is top.

If both C and D are 0 you will get a spot on the center shadow.
If you adjust C and D equally, let’s say -shadow 100×10+10+10 you will get shaddow in the bottom right corner.
If you adjust C and D equally, let’s say -shadow 100×10-10-10 you will get shaddow in the top left corner.

-background none Will make the image background transparent.

-compose DstOver is a composition method so as to avoid the need to swap the order of the two images.

-flatten I assume this “flattens” a.k.a mergest the image with the background.

-compose Over Not actually sure what this does but it is recommended 😅

output.png Your image result.

Limitations

The shadow is not dependent on the image size. So even if you have a super-large or a tiny picture they will both have the same shadow width… Bummer 😕 Although a crafty scripting guru would easily handle this with some CLI moves.

Mimicking Greenshots drop shadow

Since I use Greenshot a lot I tried to mimic the same properties of the shadow Greenshot makes. Great if you need to bulk add shadows to a lot of images.

magick convert input.png ( -clone 0 -background black -shadow 58x3.5+2.5+2.5 ) -reverse -background none -layers merge +repage output.png

Making a button for Direcotry Opus

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
    <label>Add drop shadow to image</label>
    <function type="batch">
        <instruction>magick convert {file} ( -clone 0 -background black -shadow 58x3.5+2.5+2.5 ) -reverse -background none -layers merge +repage {file|noext}-edit.png</instruction>
    </function>
</button>

Sources:

http://www.imagemagick.org/Usage/blur/#shadow