Composite Different Size Images

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
paulthiggins
Posts: 4
Joined: 2012-06-06T02:02:21-07:00
Authentication code: 13

Composite Different Size Images

Post by paulthiggins »

Hi,

I'd like to use "convert" (or whatever) to combine two different sized images. I'd like them to be aligned at the bottom left corners. For example, I have two images:

trans_alpha.png (a transparent 42x37 blank image) and shadow.png (a 68x23 image, which I want overlaid on trans_alpha.png aligned at the bottom left)

The result I'd like would be a 68x37 image, NOTE these sizes are examples only, I don't want to put the size into the command line, I just want to use the sizes from the input images (since I would like a method I can apply to any pair of images).

I have tried a lot of combinations without success:

(close, but aligned to top left, not bottom left)

Code: Select all

convert trans_alpha.png -background none shadow.png -gravity SouthWest -layers merge  +repage result.png
(aligned correctly, but result image isn't wide enough)...

Code: Select all

convert trans_alpha.png shadow.png -gravity SouthWest -composite result.png
Hopefully that makes sense. Thanks,

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

Re: Composite Different Size Images

Post by fmw42 »

paulthiggins wrote:Hi,


(close, but aligned to top left, not bottom left)

Code: Select all

convert trans_alpha.png -background none shadow.png -gravity SouthWest -layers merge  +repage result.png
(aligned correctly, but result image isn't wide enough)...

Code: Select all

convert trans_alpha.png shadow.png -gravity SouthWest -composite result.png
Hopefully that makes sense. Thanks,

Paul
Unfortunately only -composite allows -gravity settings. The others such as canvas expanding mosaic requires you specify the -page offset to get it aligned. So you need to do a computation of the offset. Or make your two images the same size by padding appropriately with transparency so that you can use -composite. Basically for composite to work, the first image needs to be the larger one, so just pad it to be your final size using -fill none -gravity ... -extent finalsize.

see
http://www.imagemagick.org/Usage/layers/#mosaic
http://www.imagemagick.org/Usage/crop/#extent

But don't you want the shadow underneath. If so swap your two images.

If you still have trouble, post links to your two example images.
paulthiggins
Posts: 4
Joined: 2012-06-06T02:02:21-07:00
Authentication code: 13

Re: Composite Different Size Images

Post by paulthiggins »

Thanks Fred, that all makes sense, I can just calculate the new size like you say, I was hoping to avoid that but it's not such a big deal. Regarding the shadow, what I'm trying to achieve is to expand the shadow image so that it's as tall as another image and as wide as the shadow image (so I can use it with the Cloudmade Leaflet map api - it uses a separate shadow image).

Thanks again for your help,

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

Re: Composite Different Size Images

Post by fmw42 »

Here is one thought, untested. You want the alignment at the bottom left, correct? If so, flip the two images vertically, use -mosiac with -page +0+0 (or leave it off), then when finished flip the result vertically back again.

convert image1 image2 -flip -background none -mosaic -flip result
paulthiggins
Posts: 4
Joined: 2012-06-06T02:02:21-07:00
Authentication code: 13

Re: Composite Different Size Images

Post by paulthiggins »

Aha, very clever - I'll give that a lash, thanks again.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite Different Size Images

Post by anthony »

fmw42 wrote:The others such as canvas expanding mosaic requires you specify the -page offset to get it aligned. So you need to do a computation of the offset.
Or you can 'flip/flop/transpose/rotate' the images do the 'layering' and undo the 'flip/...' back again. :-)

-mosaic will expand the canvas for you.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
paulthiggins
Posts: 4
Joined: 2012-06-06T02:02:21-07:00
Authentication code: 13

Re: Composite Different Size Images

Post by paulthiggins »

Hi,

Just to confirm - the following (suggested above, thanks) works:

Code: Select all

convert trans_alpha.png shadow.png -flip -background none -mosaic -flip result.png
Thanks for your help,

Paul
Post Reply