Stop image from resizing when adding shadow [solved]

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
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Stop image from resizing when adding shadow [solved]

Post by jauson »

Is there a way to lock the canvas size so it doesnt resize the image when adding a shadow?

Images used.
http://i.imgur.com/wCz2tMf.png (catssmall.png)
http://i.imgur.com/QDD0w4K.jpg (testbg.jpg)
http://i.imgur.com/VNNAMIb.png (cats.png)

Code: Select all

convert testbg.jpg \
    \( catssmall.png -set page +258+249 \)  \
    \( +clone  -background gray5 -shadow 80x20+20+20  \)  +swap -background none   \
    \
    -layers merge  +repage \
    output-shadow.jpg
As you can see, the shadow adds the black area, what I'd like it to do is just create the shadow and stop at that bg images canvas size.
Image

Also, when adding a second image and moving it, is there a way to not resize the image, but allow the image to move semi out of bounds? Crop isn't really a valid option for me.

Code: Select all

convert testbg.jpg \
    \( cats.png -set page +258+249 \)  \
    \
    -layers merge  +repage \
    output-move.jpg

Image

Solution:
Use "-layers flatten"
Last edited by jauson on 2015-06-14T02:40:50-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Stop image from resizing when adding shadow

Post by fmw42 »

You need to expand the canvas to have a shadow. You can pre-crop or resize the image by the amount of size change due to the shadow, so the result is the same size as your original

Perhaps I misunderstand what you are trying to accomplish? If so, please explain further.
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Stop image from resizing when adding shadow

Post by jauson »

i'd like to freeze the canvas size from expanding
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Stop image from resizing when adding shadow

Post by Bonzo »

i'd like to freeze the canvas size from expanding
As sibgo said you can not do that. You can either resize the image before adding the shadow or after.

The black area is caused as jpg does not support transparency. To stay as a jpg you would neede to add another background below the shadow the colour of your page and then save as a jpg.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Stop image from resizing when adding shadow

Post by snibgo »

jauson wrote:As you can see, the shadow adds the black area, what I'd like it to do is just create the shadow and stop at that bg images canvas size.
What version of M are you using? It would be helpful if you supplied your input images, so people can try to reproduce the problem.

On v6.9.1-0 on Windows 8.1 ...

Code: Select all

convert ^
  -size 600x400 xc:blue ^
  ( rose: -set page +258+249 ) ^
  ( +clone  -background gray5 -shadow 80x20+20+20 )  +swap -background none ^
  -layers merge  +repage ^
   o.jpg

identify o.jpg
... I don't get a black line, and the output is the same size as the background image.
snibgo's IM pages: im.snibgo.com
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Stop image from resizing when adding shadow

Post by jauson »

I updated the original post with the images used. I understand the reason why the black area is there, it's a jpg image.

Using Version: ImageMagick 6.9.0-10 Q16 x64. Windows 8.1.

Will get the black area.

Code: Select all

convert \
  -size 600x400 xc:blue \
  \( rose: -set page +258+349 \) \
  \( +clone  -background gray5 -shadow 80x20+20+20 \)  +swap -background none \
  -layers merge  +repage \
   o.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Stop image from resizing when adding shadow

Post by snibgo »

How are you running the command? Your syntax is bash. Do you use Cygwin?

Anyhow, the problem is simply that you ask for a shadow that will extend beyond the background image, so the result is larger. The cure is to make a smaller shadow, or crop, or use "-layers flatten" instead of "-layers merge".
snibgo's IM pages: im.snibgo.com
jauson
Posts: 36
Joined: 2015-05-04T21:05:35-07:00
Authentication code: 6789

Re: Stop image from resizing when adding shadow

Post by jauson »

snibgo wrote:How are you running the command? Your syntax is bash. Do you use Cygwin?

Anyhow, the problem is simply that you ask for a shadow that will extend beyond the background image, so the result is larger. The cure is to make a smaller shadow, or crop, or use "-layers flatten" instead of "-layers merge".
Thank you snibgo.

Yes, I'm using Mintty. Also, "flatten" did the trick. I'll have to look up what the difference of flatten and merge is.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Stop image from resizing when adding shadow [solved]

Post by snibgo »

snibgo's IM pages: im.snibgo.com
Post Reply