Shadow for transparent animated gif

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?".
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Shadow for transparent animated gif

Post by yakobom »

Hi,
I've been struggling with creating a non-linear shadow for a transparent gif. I would like it's shape to look a little like this, which was made for png:
http://www.imagemagick.org/Usage/distor ... shadow.jpg

My main problem seems to be that the shadow is not at the same place it needs to be for all images, there is a dependency on the size or something like that.
The best I came out with was this, for example:

Code: Select all

convert http://images.clipartpanda.com/superman-cape-flying-UltimatePowersSuperman2.gif -coalesce -write mpr:images  -background '#5E5E59' -shadow 55x12-20-10 -virtual-pixel Transparent -distort AffineProjection '0.7,-0.1,-0.1,0.2,230,270' -resize 115% -bordercolor Transparent -border 0 null: mpr:images -layers Composite -fuzz 2%  +repage test.gif
Any idea on what's the best way to achieve this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shadow for transparent animated gif

Post by snibgo »

The result looks fine to me. What don't you like about it?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shadow for transparent animated gif

Post by fmw42 »

Also, looks reasonable to me on IM 6.9.2.6 Q16 Mac OSX. What version of IM and what platform are you using? Perhaps you need to upgrade.
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Re: Shadow for transparent animated gif

Post by yakobom »

I am using version 6.9.2-5 which I have built on Ubuntu 14.04, but I'm pretty sure it is a matter of my understanding and not a problem with the version.
I want the shadow to be projected to the right bottom, pretty much like it is in the code I've posted. My problem is that on other image it looks different - for example with the same code but another image, the shadow is above the actual image:

Code: Select all

convert http://orig08.deviantart.net/03fe/f/2013/037/f/d/cry__poke_by_kiwa007-d5u29cm.gif -coalesce -write mpr:images  -background '#5E5E59' -shadow 55x12-20-10 -virtual-pixel Transparent -distort AffineProjection '0.7,-0.1,-0.1,0.2,230,270' -resize 115% -bordercolor Transparent -border 0 null: mpr:images -layers Composite -fuzz 2%  +repage test.gif
Any advice on how to ensure the location of the shadow will be appreciated.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shadow for transparent animated gif

Post by snibgo »

... on other image it looks different ...
Please post that image.
snibgo's IM pages: im.snibgo.com
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Re: Shadow for transparent animated gif

Post by yakobom »

I have posted the entire code for both images - the first in my original post, and the second in my reply. Please let me know if this is not good enough...
Just run the second piece of code and you will see the result.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shadow for transparent animated gif

Post by fmw42 »

Look more carefully at
http://www.imagemagick.org/Usage/distorts/#affine
http://www.imagemagick.org/Usage/distor ... projection
http://www.imagemagick.org/Usage/distorts/#shadow3d.

There he uses +distort Affine and the arguments dictate the direction of the shadow and are image size dependent. You can remove the size issue by computing those arguments (control points) using %w and %h as arguments rather than fixed values.

I suppose you could do the same with AffineProjection, that is, compute the arguments from %{fx: ...] computations.
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Re: Shadow for transparent animated gif

Post by yakobom »

Thanks - but this is the thing actually. I have looked there prior to posting - I am seeking a way to do this without the size dependency, as I do not know on which images this action will be applied...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shadow for transparent animated gif

Post by fmw42 »

If you use calculations for the arguments, the size will be taken into account. Seems to me to be easier to work with control points, so as to control the offset easier. But it is too late here for me to think straight. Perhaps tomorrow. Or one of the other uses, who are in a different time zone, might help before then.
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Re: Shadow for transparent animated gif

Post by yakobom »

Hi,
Yes, this is exactly what I want - to disconnect the dependency on the size. Any help will be highly appreciated.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Shadow for transparent animated gif

Post by snibgo »

Calculating the AffineProjection parameters from anything else is quite complex. IM can calculate them from control points, so that seems the obvious solution.

I suggest you create 2 or 3 examples, on paper or in Gimp, from images of different sizes with the object to be shadowed in different positions. For each example, where do you want the shadow?

From those examples, you can work out the control points. Perhaps the same control points can be used for all images.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shadow for transparent animated gif

Post by fmw42 »

The problem is that you want your shadow down and to the right of the bounds of the image. The -layers composite does not allow you to expand the image beyond its bounds. So you have to -extent your image to accommodate the shadow. Here is an example. (Unix syntax)

Code: Select all

convert http://orig08.deviantart.net/03fe/f/2013/037/f/d/cry__poke_by_kiwa007-d5u29cm.gif \
-coalesce -write mpr:img -delete 0--1 \( mpr:img -gravity northwest -extent 200x200% \) \
null: \( mpr:img -background '#5E5E59' -shadow 55x12-20-10  \
-flip -virtual-pixel Transparent +distort SRT "%w,%h 0.75,0.25 0 %[fx:1.25*w],%[fx:1.25*h]" \) \
-background transparent -layers composite -trim +repage animation.gif
Image

All the controls are in the +distort SRT. See http://www.imagemagick.org/Usage/distorts/#srt for the arguments. I have flipped the image first, then applied +distort SRT. The first and last pair of coordinates account for the shift right and downward in proportion to the image size by 25% to the right and 25% to the bottom. The second and third arguments are the scaling (0.75,0.25 is 75%w,25%h) and the fourth 0 is no rotation. You mostly need to decide what scaling your want for the shape of the shadow and then just set the last two arguments in the %[fx: ...] to account for the shift right and shift downward. Note I expanded the image by 200% to make plenty of room for the shadow. If you shift the image too much (beyond a factor of two for the bottom), -layers composite will object. You can adjust the -extent to minimize the size, but need to allow enough room for the shadow. The -trim at the end removes the excess from the extent. You can use -border to pad it out some if you want.

I also swapped the images so that the extended original images were first in the command line and the shadow overlays were done after the null:

Note your -fuzz 2% at the end did nothing without a -trim.

Modify this as you desire.
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Re: Shadow for transparent animated gif

Post by yakobom »

Hi,
Thank you very much for a great answer. It was VERY helpful and extensive.
One thing that I do not understand in the result is that the shadow's transparency seems weird - in the part of the original image it looks ok, and in the extended part it does not. Look here, for example:
Image

Any idea what's missing in the command?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shadow for transparent animated gif

Post by fmw42 »

try just flattening the image agains some background color, say white or some other light color at the end and see if that makes it look better. Your shadow now overlays some white and some transparent, so the backgrounds are different.

add

-background white -compose over -flatten

just before writing the output.

But why do you want the shadow where you have it. It does not look like an appropriate shadow so much underneath the boy. It needs to be offset some more downward.
yakobom
Posts: 27
Joined: 2015-10-27T22:40:37-07:00
Authentication code: 1151

Re: Shadow for transparent animated gif

Post by yakobom »

I get it, thanks. I did not finish playing with the offset, I just bumped into this problem and wanted to solve it first.
Again, many thanks!
Post Reply