Having trouble overlapping images with blur and shadow

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
jjnKxffoddpMuwX
Posts: 4
Joined: 2018-08-05T10:47:14-07:00
Authentication code: 1152

Having trouble overlapping images with blur and shadow

Post by jjnKxffoddpMuwX »

What I’m trying to do is, given an image:
Image

Produce a “presentation” of it in a specific format:
Image

The input image can be of any dimensions, vertical or horizontal. The result is:
  • A canvas of 800x600.
  • The background is the image resized proportionally, blurred, centred, and filling the whole available area as tightly as possible. So if the original is 5000x4000, the background would be 800x600; if the original is 1500x2400, the background would be 1125x600.
  • The top image is resized proportionally to a maximum of 640x480 and centred. It has a 10px white border and a drop shadow.
I’ve been using another post as a guide, but even looking at the documentation I don’t get everything that’s going on. How the images stack works with clone is quite confusing. Best I could get so far is:

Code: Select all

convert input.png  \
\( -clone 0 -blur 0x20 -gravity center -resize 800x -crop 800x600+0+0 \) \
\( -clone 0 -resize 640x480 \) \
\( -clone 1 -fill white -colorize 100 \) \
\( -clone 2 -fill black -colorize 100 \) \
\( -clone 3,4 -gravity center -compose over -composite \) \
-delete 0,3,4 \
\( -clone 0,2 -gravity center -compose multiply -composite \) \
-delete 0,2 +swap \
-gravity center -compose over -composite \
result.png
Which results in:
Image

Thank you for any help.
Last edited by jjnKxffoddpMuwX on 2018-08-05T11:18:57-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Having trouble overlapping images with blur and shadow

Post by fmw42 »

Sorry, but I am confused about how ending sizes should be computed given the size of the input. Can you explain a bit better with one or two actual image sizes or by percent scaling values?

You start by saying the canvas should be 800x600. But then you say the background should be 800x640.

Are you saying you just want to take a large image and resize in proportion to a max of 800x600 or 800x640 and blur it?

Similarly you want to resize proportionally to 640x480 and then put a white border on it and insert that into the center of the blurred background.

An issue potentially have is that you may have either a portrait or landscape format input, but you want the blurred backgound to be the opposite? Is that correct. Your result above here does that. So please clarify whether the input could be either portrait or landscape and what format the blurred background should be.

Please also identify your Imagemagick version and platform. (Please do this always when asking questions since syntax may vary).

Also could you post an example of your large input image so we can test with it? Or does it matter what the input size is? Could it be smaller than the output sizes that you want?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Having trouble overlapping images with blur and shadow

Post by fmw42 »

This will reproduce the effect you want for this image. But you have to know whether the input is landscape or portrait and likewise about the background. I have only created this command for this one situation where you have a portrait input and want a landscape background/output

Code: Select all

convert input.jpg -write mpr:img +delete \
\( mpr:img -resize 800x -crop 800x600+0+0 +repage -blur 0x50 \) \
\( mpr:img -resize x440 -bordercolor white -border 10 \) \
-gravity center -compose over -composite result.jpg
Note that I sued -resize x440 rather than 480, since I need to account for the 10 pixel top and bottom border so that the final image size is 480 tall in order to fit into the 600 pixel tall background.
jjnKxffoddpMuwX
Posts: 4
Joined: 2018-08-05T10:47:14-07:00
Authentication code: 1152

Re: Having trouble overlapping images with blur and shadow

Post by jjnKxffoddpMuwX »

fmw42 wrote: 2018-08-05T11:15:39-07:00 You start by saying the canvas should be 800x600. But then you say the background should be 800x640.
That was a mistake, sorry. Fixed. It should be 800x600.
fmw42 wrote: 2018-08-05T11:15:39-07:00 Are you saying you just want to take a large image and resize in proportion to a max of 800x600 [snip] and blur it?
More accurately, I want to resize in proportion to a minimum of 800x600. So basically one of the sizes touches the canvas and the other is cut off, thus filling the whole area.
fmw42 wrote: 2018-08-05T11:15:39-07:00 you want to resize proportionally to 640x480 and then put a white border on it and insert that into the center of the blurred background.
Yes. This time to a maximum of 640x480 (what -resize does).
fmw42 wrote: 2018-08-05T11:15:39-07:00 So please clarify whether the input could be either portrait or landscape and what format the blurred background should be.
Yes, the input could be either portrait or landscape. As for the background, I hope the previous clarification is enough.
fmw42 wrote: 2018-08-05T11:15:39-07:00 Please also identify your Imagemagick version and platform. (Please do this always when asking questions since syntax may vary).
Apologies for the omission. Imagemagick 7.0.8-8 (built with Homebrew), on macOS 10.12.6.
fmw42 wrote: 2018-08-05T11:15:39-07:00 Could it be smaller than the output sizes that you want?
Yes. If it scales up that’s fine.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Having trouble overlapping images with blur and shadow

Post by fmw42 »

Try this variation. Note that IM 7 uses magick rather than convert. See http://imagemagick.org/script/porting.php#cli

Code: Select all

magick input.jpg -write mpr:img +delete \
\( mpr:img -resize 800x600^ -crop 800x600+0+0 +repage -blur 0x50 \) \
\( mpr:img -resize 600x440 -bordercolor white -border 10 \) \
-gravity center -compose over -composite result.jpg
Note that the ^ on the first resize means scale according to the smaller dimension of the image. See https://www.imagemagick.org/script/comm ... p#geometry
jjnKxffoddpMuwX
Posts: 4
Joined: 2018-08-05T10:47:14-07:00
Authentication code: 1152

Re: Having trouble overlapping images with blur and shadow

Post by jjnKxffoddpMuwX »

Thank you very much, fmw42. Only thing missing is the drop shadow on the top image. I tried it on my own but it’s harder than it looked.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Having trouble overlapping images with blur and shadow

Post by fmw42 »

Sorry, I missed that since I did not see it in your example result. Try this

Code: Select all

magick input.jpg -write mpr:img +delete \
\( mpr:img -resize 800x600^ -crop 800x600+0+0 +repage -blur 0x50 \) \
\( mpr:img -resize 600x440 -bordercolor white -border 10 \) \
\( +clone -background black -shadow 80x3+5+5 \) \
\( -clone 2,1 -background none -layers merge +repage \) \
-delete 1,2 \
-gravity center -compose over -composite result.jpg
jjnKxffoddpMuwX
Posts: 4
Joined: 2018-08-05T10:47:14-07:00
Authentication code: 1152

Re: Having trouble overlapping images with blur and shadow

Post by jjnKxffoddpMuwX »

Three more lines to do a shadow is not something I’d get to on my own. ImageMagick is one of the most powerful and confusing CLI tools I’ve ever used. Even looking at the documentation, I don’t understand everything about the code, like what mpr:img does or how clone actually works and why it’s needed. But after a few more tweaks, I think I’m finally happy with it.

Code: Select all

magick input.png -write mpr:img +delete \
\( mpr:img -gravity center -resize 800x600^ -crop 800x600+0+0 +repage -blur 0x16 \) \
\( mpr:img -resize 640x480 -bordercolor white -border 6 \) \
\( +clone -background black -splice 40x40 -shadow 30x20-20-20 \) \
\( -clone 2,1 -background none -layers merge +repage \) \
-delete 1,2 \
-gravity center -compose over -composite result.png
Thank you again for the great help, fmw42.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Having trouble overlapping images with blur and shadow

Post by fmw42 »

MPR is an in-memory format that uses names rather than numbers. Otherwise it is pretty much the same as using clones. The shadow documentation is at

https://www.imagemagick.org/Usage/reference.html

which points to

https://www.imagemagick.org/Usage/blur/#shadow
Post Reply