Elongated “Shadow” Script

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
drdaanger
Posts: 5
Joined: 2019-01-11T07:51:43-07:00
Authentication code: 1152

Elongated “Shadow” Script

Post by drdaanger »

I am trying to duplicate the shadow effect seen on the green icon here:
Image
using command line imagemagick, version 7.0.8-34 on Mac. The drop shadow is pretty straightforward with -shadow command. The elongated shadow is giving me troubles.

Given the inputs foreground.png (That has transparent bits to it, and the alpha channel set), background.png, and size…

The first strategy I tried to use was a custom -fx to output the maximum alpha value between the source pixel and the pixel immediately west and north of the source pixel:

Code: Select all

magick foreground.png -channel A -fx 'min(max(p.a, p[-1,-1].a),0.04)' -channel-fx 'red=0 green=0 blue=0' shadow.png
The problem here is that the p[-1,-1] refers to the source image, not the copy being manipulated. I suspect it isn’t possible to reference the being-manipulated copy. So I could just that run that command size - 1 times. Not fun.

The second strategy relies on -motion-blur. Motion blur the foreground in a certain direction, even out the alpha, then blacken the result:

Code: Select all

 magick foreground.png -channel-fx 'red=0 green=0 blue=0' -channel A -motion-blur "0x$magicSigma+215" foreground.png -fx 'ceil(u.a) - 0.96 - (0.04 * v.a)' -blur 0x2 shadow.png
The problem I'm running into there is getting the right magicSigma. If the magicSigma is too high, it blurs too much out to the sides, so I lose the elongated shadow effect. If it’s too low, the shadow doesn’t reach to the southwestern corner.

Any suggestions as to how I can improve either method?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Elongated “Shadow” Script

Post by fmw42 »

I do not see any elongated shadow and only a hint of any shadow in the white area surrounding the green. Please clarify or provide a better example. Is the elongated shadow what I see as darker green in the green area?

Please, always provide your IM version and platform when asking questions, since syntax may differ.

Please also provide your input imagery so we can test your commands or try other things

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Elongated “Shadow” Script

Post by snibgo »

I can see two shadow effects in the OP image:

1. Applied to the white object, with the shadow over the green rectangle.

2. Applied to the green rectangle, with the shadow over the white background.

So the result has five layers, from back to front: white background, shadow, green rectangle, shadow, white object.

The OP mentions foreground.png and background.png, but doesn't show these, and doesn't use foreground.png in any command.
snibgo's IM pages: im.snibgo.com
drdaanger
Posts: 5
Joined: 2019-01-11T07:51:43-07:00
Authentication code: 1152

Re: Elongated “Shadow” Script

Post by drdaanger »

In posting my reply, I discovered what my problem was (-motion-blur was adding stuff if -virtual-pixel was left to default and the icon was on an edge), but I'm still interested in hearing if there’s a better way of doing this part of the operation, specifically in setting the sigma value for the -motion-blur operation or if there’s just a better operation out there.

Code: Select all

magick foreground.png -virtual-pixel transparent -channel RGB -evaluate set 0 -channel A -motion-blur "0x100+225" foreground.png -fx 'ceil(u.a) - 0.96 - (0.04 * v.a)' -blur 0x2 shadow.png
magick background.png shadow.png -composite foreground.png -composite combined.png
turns foreground
Image
on background
Image
into a shadow
Image
and a combined icon
Image

ImageMagick 7.0.8-34 for Mac.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Elongated “Shadow” Script

Post by snibgo »

You use "-motion-blur" which is sophisticated and quite expensive. Then you use "-fx" to reduce the motion blur to a binary on/off. "-fx" is expensive.

A simpler and quicker alternative is to use "-morphology":

Code: Select all

convert ^
  1KfLe5M.png ^
  -alpha Extract ^
  -morphology Thicken:-1 "2x2+1+1:1,-,-,0" ^
  spread_bot_right.png
Image

This can be used to make a shadow that spreads in a south-east direction. Layer the shadow behind the object.

However, a bug currently prevents this working in v7 magick. See https://www.imagemagick.org/discourse-s ... =3&t=36050 . I expect the bug will be fixed in the next release.
snibgo's IM pages: im.snibgo.com
drdaanger
Posts: 5
Joined: 2019-01-11T07:51:43-07:00
Authentication code: 1152

Re: Elongated “Shadow” Script

Post by drdaanger »

This is exactly what I was looking for. Thanks!
Post Reply