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?".

Composite Different Size Images

Postby paulthiggins » 2012-06-06T02:23:55+00:00

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
paulthiggins
 
Posts: 4
Joined: 2012-06-06T02:02:21+00:00

Re: Composite Different Size Images

Postby fmw42 » 2012-06-06T09:17:16+00:00

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.
User avatar
fmw42
 
Posts: 13877
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: Composite Different Size Images

Postby paulthiggins » 2012-06-06T09:23:50+00:00

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
paulthiggins
 
Posts: 4
Joined: 2012-06-06T02:02:21+00:00

Re: Composite Different Size Images

Postby fmw42 » 2012-06-06T09:29:42+00:00

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
User avatar
fmw42
 
Posts: 13877
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: Composite Different Size Images

Postby paulthiggins » 2012-06-06T09:33:48+00:00

Aha, very clever - I'll give that a lash, thanks again.
paulthiggins
 
Posts: 4
Joined: 2012-06-06T02:02:21+00:00

Re: Composite Different Size Images

Postby anthony » 2012-06-07T19:23:40+00:00

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
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8687
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: Composite Different Size Images

Postby paulthiggins » 2012-06-12T02:10:29+00:00

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
paulthiggins
 
Posts: 4
Joined: 2012-06-06T02:02:21+00:00


Return to Users

Who is online

Users browsing this forum: Bing [Bot], Google Adsense [Bot], MSNbot Media and 17 guests