Adding shadow to 2 combined images

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Adding shadow to 2 combined images

Post by stealthrt »

Been trying for awhile now trying to get a shadow using the following code:

Code: Select all

using (MagickImage image = new MagickImage(@"C:\Users\David\Pictures\YnTf9.png"))
{
     MagickImage _shadow = new MagickImage(bitmap);

     using (IMagickImage backgroundImg = image.Clone())
     {
          backgroundImg.Blur(0, 5);
          backgroundImg.Crop(400, 300, Gravity.Center);
          backgroundImg.RePage();

          image.Resize(0, 300);
          _shadow.Resize(0, 300);
          _shadow.Shadow(10, 10, 0.8, (Percentage)80, MagickColor.FromRgb(0, 0, 0));

          backgroundImg.Composite(_shadow, Gravity.Center, CompositeOperator.SrcOver);
          backgroundImg.Composite(image, Gravity.Center, CompositeOperator.SrcAtop);
          backgroundImg.Write(@"C:\Users\David\Pictures\NEW.png");
     }
}
But the outcome looks like it originally did to begin with:
Image

And what I am looking to accomplish with the code above is:
Image

Anyone able to help me out? :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding shadow to 2 combined images

Post by snibgo »

Where does "bitmap" come from? A shadow must be a shadow of something (ie opaque pixels) but I can't see what that "something" is.

While you develop this, I suggest you write _shadow to a file. Then you can more easily find where the problem is.
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: Adding shadow to 2 combined images

Post by fmw42 »

I suggest you work in the command line first and +write tmpX.png at each step to see what it is doing. Then convert that to .NET.

Try this (I create a black in white mask image that I blur, then multiply it by the blurred cropped background image. Then I overlay the resized image onto the center of that).

Code: Select all

convert YnTf9.png \
\( -clone 0 -blur 0x10 -gravity center -crop 400x300+0+0 +repage \) \
\( -clone 0 -resize x300 \) \
\( -clone 1 -fill white -colorize 100 \) \
\( -clone 2 -fill black -colorize 100 \) \
\( -clone 3,4 -gravity center -compose over -composite -blur 0x15 -level 40x100% \) \
-delete 0,3,4 \
\( -clone 0,2 -gravity center -compose multiply -composite \) \
-delete 0,2 +swap \
-gravity center -compose over -composite \
result.png
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Adding shadow to 2 combined images

Post by stealthrt »

Hum.. that seems to come out just my own does. Its looks just like the normal image.
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Adding shadow to 2 combined images

Post by stealthrt »

Any input on the command line issue?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding shadow to 2 combined images

Post by fmw42 »

I do not understand the question. Are you saying my command line does not work for you? If so, what is your Imagemagick version?
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Adding shadow to 2 combined images

Post by stealthrt »

Yes I am saying that it does not work on my end.

The version I am using is: ImageMagick-7.0.7-26-Q16-x64-dll
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding shadow to 2 combined images

Post by fmw42 »

Change convert to magick.

Code: Select all

magick YnTf9.png \
\( -clone 0 -blur 0x10 -gravity center -crop 400x300+0+0 +repage \) \
\( -clone 0 -resize x300 \) \
\( -clone 1 -fill white -colorize 100 \) \
\( -clone 2 -fill black -colorize 100 \) \
\( -clone 3,4 -gravity center -compose over -composite -blur 0x15 -level 40x100% \) \
-delete 0,3,4 \
\( -clone 0,2 -gravity center -compose multiply -composite \) \
-delete 0,2 +swap \
-gravity center -compose over -composite \
result.png
That works fine for me on IM 7.0.7.25 Q16 Mac OSX.

Note the above is Unix syntax. For windows:

Code: Select all

magick YnTf9.png ^
( -clone 0 -blur 0x10 -gravity center -crop 400x300+0+0 +repage ) ^
( -clone 0 -resize x300 ) ^
( -clone 1 -fill white -colorize 100 ) ^
( -clone 2 -fill black -colorize 100 ) ^
( -clone 3,4 -gravity center -compose over -composite -blur 0x15 -level 40x100% ) ^
-delete 0,3,4 ^
( -clone 0,2 -gravity center -compose multiply -composite ) ^
-delete 0,2 +swap ^
-gravity center -compose over -composite ^
result.png
See https://www.imagemagick.org/Usage/windows/
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Adding shadow to 2 combined images

Post by stealthrt »

Still doesnt seem to be working for me :(

Image
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Adding shadow to 2 combined images

Post by stealthrt »

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

Re: Adding shadow to 2 combined images

Post by fmw42 »

I am not a Windows user, so do not know what might be wrong. But if you are running this is .bat file, then double the % to %%
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: Adding shadow to 2 combined images

Post by stealthrt »

Yep that was the issue. Not having the %% was the issue. Now having to convert it to C# :/
Post Reply