Page 1 of 1

Composite Different Size Images

Posted: 2012-06-06T02:23:55-07:00
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

Re: Composite Different Size Images

Posted: 2012-06-06T09:17:16-07:00
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.

Re: Composite Different Size Images

Posted: 2012-06-06T09:23:50-07:00
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

Re: Composite Different Size Images

Posted: 2012-06-06T09:29:42-07:00
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

Re: Composite Different Size Images

Posted: 2012-06-06T09:33:48-07:00
by paulthiggins
Aha, very clever - I'll give that a lash, thanks again.

Re: Composite Different Size Images

Posted: 2012-06-07T19:23:40-07:00
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.

Re: Composite Different Size Images

Posted: 2012-06-12T02:10:29-07:00
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