Page 1 of 1

How do I scale a composite overlay to fill a background?

Posted: 2009-04-10T00:36:09-07:00
by arber6
I have a number of photos (backgrounds) with various aspect ratios that I want to resize (reduce) to a height of 400, which means the width will vary with each photo. I also want to dissolve a logo image (overlay) with some transparency over each image (before or after the resize), so that the logo is as large as possible, but still fits within the background image.

I'm using the Command Line composite command in a Windows command prompt, but I can't work out how to scale the overlay to fit the background image, without cropping the overlay (with certain background aspect ratios).

The following code works well for squarish (1:1), background images, but for narrow background images, the logo gets cropped at the left and right, and for short background images, the logo gets cropped at the top and bottom.

Code: Select all

composite -gravity center logo.png[390x] pic1.jpg[x400] -dissolve 30 test1.jpg

Re: How do I scale a composite overlay to fill a background?

Posted: 2009-04-10T11:32:31-07:00
by fmw42
see -resize and geometry flags

http://www.imagemagick.org/script/comma ... php#resize
http://www.imagemagick.org/script/comma ... p#geometry

and/or provide an example of input images, processing commands and output image

Re: How do I scale a composite overlay to fill a background?

Posted: 2009-04-10T22:58:04-07:00
by anthony
Scaling an overlay to match an image of unknown size, can be tricky. The only real solution is to somehow read the image dimensions and then scale (resize) the overlay to match.

IM can even do the mathematics for you using a %[fx:...] expression as part of the identify output.

So for example to calculate a overlay width that is 1/5 of the images width you can do something like...

Code: Select all

  overlay_width=`identify -format '%[fx:w/5]' image.png
  convert image.png \( overlay.png -resize "${overlay_wdith}x0" \) \
              -gravity SouthEast -geometry +5+5  -composite   result.png
NOTE the -geometry can do a resize of just the overlay image, at the same time it is setting the overlays offset, but I do not recommend this as the resize and offset are really two very different this (resize operator, and the composite offset setting).

Re: How do I scale a composite overlay to fill a background?

Posted: 2009-04-11T01:35:28-07:00
by arber6
Thanks for your replies. I did some digging, and decided to explore the ImageMagickObject approach from VBScript - that's where I was making batch calls from anyway.
I'm much more familiar with VB, so reading the background properties and adjusting the overlay size using script is going to be easier for me.
That said, I imagine being able to "fit" an overlay to a background would be a useful argument for the composite command.