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

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
arber6

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
arber6

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

Post 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.
Post Reply