Drop Shadow real problem, I searched but no help :(

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
sodevrom

Drop Shadow real problem, I searched but no help :(

Post by sodevrom »

Hello,
I have been studying the image magick examples and forums for 6 hours now and I just can't manage to do what I want.
I attached the below picture to show you want I want. I don't want the flip horizontaly/alpha effect but I want the drop shadow effect.
Image

From what I have read I would need to use the distortion and also shadow commands, but I just can't get the right drop shadow effect. Can someone please help me with this ? I would appreciate it a lot,

Thank you very much
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Drop Shadow real problem, I searched but no help :(

Post by anthony »

As the shadow would not change from box image to box image, I would suggest you just draw the shadow in the right color then blur it. You can then overlay the other parts of the image over that shadow.

Simple.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sodevrom

Re: Drop Shadow real problem, I searched but no help :(

Post by sodevrom »

Hello,
The shadow will change, the shadow needs to apply to any form, not only a box. This is just a simple example of what I want.
Any ideeas :( ?

Thanx
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Drop Shadow real problem, I searched but no help :(

Post by fmw42 »

In my opinion, IM is not the tool for automating a real shadow the way you want for an arbitrary shaped object from any perspective. You need a 3D graphics program. But perhaps Anthony or others have some other ideas.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Drop Shadow real problem, I searched but no help :(

Post by anthony »

If you can get the shape of the shadow (all that matters)
then it is posible to distort it so it looks like it is falling onto
the surface.

Once you have the shape on the surface (or even before) you can
then blur it.



However surface shadows can be tricky when you also want penumbra effects. That is the shadow becoming more blurred with distance from the object casting it.

This requires the use of a variable blur, and that is something that may be posible within the IM library but is not currently implemented. Sorry....

Fred Weinhaus (who posted above me) has experimented with variable blur generation in his scripts, both by selecting from an array of progressivally more blurred images 'varibleblur', as well as in generating rotational blurs by averaging multiple images 'zoomblur'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sodevrom

Re: Drop Shadow real problem, I searched but no help :(

Post by sodevrom »

Any chance to help me with a little piece of code, I am not very good with IM and I really want to get this done.
I am trying non stop to make this, but with no results :(
I am using the commandline version ...
Thanx
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Drop Shadow real problem, I searched but no help :(

Post by anthony »

Do you have an example of an odd shape image?
And a rough outline of how you what its shadow to fall?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sodevrom

Re: Drop Shadow real problem, I searched but no help :(

Post by sodevrom »

Hello, so here is a sample transform :
www.sodevrom.net/sam.jpg

Here is the original png :
www.sodevrom.net/sampng.png

I don't know how to code this and I would appreciate if someone could help.
My ideea would be to create the shadow in a different layer, after that modify the geometry (tilt, perspective) of the shadow and after that merge the 2 layers. Easy to say, hard to do :(

Thanx
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Drop Shadow real problem, I searched but no help :(

Post by anthony »

sodevrom wrote:Hello, so here is a sample transform :
http://www.sodevrom.net/sam.jpg

Here is the original png :
http://www.sodevrom.net/sampng.png
Thanks for both example images
My ideea would be to create the shadow in a different layer, after that modify the geometry (tilt, perspective) of the shadow and after that merge the 2 layers. Easy to say, hard to do :(
Your idea is spot on. but with one small twist.

The shape is standing on the ground on the bottom most row of pixels. rather than trying to calculate and do all your calculations basied on this position 'flip' the image so that the ground 'row' is at the top of pixel '0'.

This simplifies the working, as you only need to do the appropriate 'shear' at origin or '0,0'. Though the intermedite working images will be upside down...

Here is my final solution...

Code: Select all

   convert sampng.png -flip \
          \( +clone -background Black -shadow 60x10+0+0 \
              +distort Affine '0,0 0,0  100,0 100,0  0,100 -100,50' \) \
         +swap -background white -layers merge \
         -flip -fuzz 2% -trim +repage \
         output.png
Each line is a processing step as follows...
  • flip image so ground is at 0,0
  • create 'blurred shadow' preserves 'layer offsets
  • shear distort the shadow, along the Y=0 line.
    The numbers define the final angles and so not relate in any way to the size or shape of the image, only to the final 'shearing' effect wanted.
    In many ways this is just like a simple -shear (see Warping Images, Shear, but using +distort so that it preserves and uses 'layer offsets, as part of the warping process.
    NOTE: the size of the distorted image is larger than expected, due to the 'cut off corners. It also generates large negative offsets, so be careful saving and viewing the resulting layer image.
  • place shadow layer behind the shape and merge to form a new layer, just big enough to hold all images. Also add a white background. Note layer images will have negative offsets!
  • Final clean up. flip image back, trim the excess image generated from distorting a rectangular image, and junk the layer offsets.
And you have a nice affine warped shadow.

You can adjust the shadow any way you like simply by adjusting the
movement of the 0,100 control point, by changing the last two numbers only. I move it left -100 pixels, and compress it vertically to 50 to get a 'compress shear'.


Rather than flipping, a variation is to just set the ground line of the image using

Code: Select all

     sampng.png -flip +distort SRT '0,0 1,-1 0'
this flips the image, then 'layers' flips the image so that Y=0
along the bottom row. The final '-flip' is now not needed, and images remain right way up.

Code: Select all

convert sampng.png -flip +distort SRT '0,0 1,-1 0' \
          \( +clone -background Black -shadow 60x10+0+0 \
              +distort Affine '0,0 0,0  100,0 100,0  0,100 -100,50' \) \
         +swap -background white -layers merge \
         -fuzz 2% -trim +repage \
         output.png
Note you may have to change the direction of the shear to get the shadow as you want.

Addendum.... I have now added this to IM Examples, 3d Shadows, using Affine Shears which should appear in a day or so.

Also the example is taken further with an additional shadow processing to provide variable blurring so that the shadow becomes more blurry the further it gets from the base line of the shape.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sodevrom

Re: Drop Shadow real problem, I searched but no help :(

Post by sodevrom »

Hello,
Finally I have my internet back. My ISP was down for quite some days and I was not able to read the forum.
As soon as it went back up, I checked the post.

First, thank you a lot for the great explanation ! Thank you !
There is a problem, the script is simply not working for me. I am recieving the folowing errors :

convert: unable to open image `1,-1': No such file or directory @ blob.c/OpenBlo
b/2427.
convert: unable to open image `0'': No such file or directory @ blob.c/OpenBlob/
2427.
convert: invalid argument for option Affine : 'require at least 1 CPs' @ distort
.c/GenerateCoefficients/440.
convert: unable to open image `0,0': No such file or directory @ blob.c/OpenBlob
/2427.
convert: unable to open image `100,0': No such file or directory @ blob.c/OpenBl
ob/2427.
convert: unable to open image `0,100': No such file or directory @ blob.c/OpenBl
ob/2427.
convert: unable to open image `-100,50'': No such file or directory @ blob.c/Ope
nBlob/2427.


What can be the cause of this ?
I am running the script from command prompt (windows) and I wrote it all in one line.

Thank you, and I hope you can help me again :)

Keep on the great work
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Drop Shadow real problem, I searched but no help :(

Post by el_supremo »

Change all the single quotes to double quotes. For example, '0,0 1,-1 0' should be "0,0 1,-1 0"
And make sure you haven't left out a quote.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Drop Shadow real problem, I searched but no help :(

Post by anthony »

The quote change is needed for DOS scripts. See UNIX shell to Dos Script conversion details in
IM Examples, API, Windows DOS scripting.
http://www.imagemagick.org/Usage/api/#windows
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sodevrom

Re: Drop Shadow real problem, I searched but no help :(

Post by sodevrom »

Everything is perfect now. I made the background transparent (so I can save it as PNG) but I have one last question ( I promise)
How can I make the shadow smaller ?
I played with the values and I set the shadow in the opposite side (so in the left) and it's ok. But if I want to make it smaller, how can I do it ?
Here is an example of what I mean by shadow size :
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Drop Shadow real problem, I searched but no help :(

Post by fmw42 »

To change the shadow height, add -resize 100xXX% as follows (if XX=100 you should get the original shadow, but if you make it smaller, then the shadow height will be smaller)

Anthony's original example from http://www.imagemagick.org/Usage/distorts/#shadow3d

standing_shape.png
Image

change this:
convert standing_shape.png -flip +distort SRT '0,0 1,-1 0' \
\( +clone -background Black -shadow 60x5+0+0 \
-virtual-pixel Transparent \
+distort Affine '0,0 0,0 100,0 100,0 0,100 100,50' \
\) +swap -background white -layers merge \
-fuzz 2% -trim +repage standing_shadow.jpg
Image

to this:
convert standing_shape.png -flip +distort SRT '0,0 1,-1 0' \
\( +clone -background Black -resize 100x75% -shadow 60x5+0+0 \
-virtual-pixel Transparent \
+distort Affine '0,0 0,0 100,0 100,0 0,100 100,50' \
\) +swap -background white -layers merge \
-fuzz 2% -trim +repage standing_shadow2.jpg
Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Drop Shadow real problem, I searched but no help :(

Post by anthony »

The -resize is redundant. Not needed as resize (or scaling) is part of the Affine distortion method....

Fred, you should know better :-)

The last number in the affine distort '50' is the final height of the shadow.

to make it smaller make that number smaller. To make it less long, rather than just reducing the height, reduce both the '100,50' values...

For example to get the same effect of the original resize use a value of '100,33' instead of '100,50'.

Try to map the control point on the original image and plot how they move and you should be able to see and understand how it all works.

The second example added to IM examples with variable shadow,
http://www.imagemagick.org/Usage/distorts/#shadow3d_var
is only slightly more complex. In that two distorts are used, one before the bluring
and the final one that positions the shadow in the same way as before. However there are two control points that need to be adjusted to control the final height and length of the shadow, in the final distortion.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply