Resizing overlay image at the same time as overlaying?

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
b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Resizing overlay image at the same time as overlaying?

Post by b3and1p »

I am unable to figure out how to do this from the manual, maybe someone here can shed some light?

I want to multiply image A over image B. Image A is a different resolution from image B, so I want to resize A to match B and then multiply it over in a single line. Is this possible? Do I need to know the resolution of A before I write out the line? I was hoping there was a "fit" resize method. Thanks everyone!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resizing overlay image at the same time as overlaying?

Post by anthony »

Code: Select all

compose  imageB.png \( imageA.png -resize  WxH \) -composite result.png
You can use geometry for this too, though it is not recommended.

Code: Select all

compose imageB.png imageA.png -geometry XwH -composite result.png
Or using the specialise composite command... note the image order (overlay first)

Code: Select all

composite imageA.png imageB.png -geometry XwH -composite result.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Resizing overlay image at the same time as overlaying?

Post by b3and1p »

Is there a way of doing this if you don't know the resolution of image A?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing overlay image at the same time as overlaying?

Post by fmw42 »

In a script or two line command, you can extract the size in one command and use it in the second.

size=`convert imageB -format "%wx%h" info:`
convert imageB.png \( imageA.png -resize "$size" \) -compose multiply -composite result.png

This will compute the size of imageB, then resize imageA to the size of imageB, then multiply the two to composite them.

see string formats at http://www.imagemagick.org/script/escape.php


the above is for unix, for windows see http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resizing overlay image at the same time as overlaying?

Post by anthony »

If you know thw resolution you want you can do a -resample Which is really just a resize to a specific resolution. See IM Examples, Resample
http://www.imagemagick.org/Usage/resize/#resample
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Resizing overlay image at the same time as overlaying?

Post by b3and1p »

Thanks everyone you guys rock! I appreciate the help very much.
b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Resizing overlay image at the same time as overlaying?

Post by b3and1p »

For anyone else taking a look at this thread there is one thing I had to change in the command. -resize widthxheight\!

the \! made sure to ignore the orginal file's aspect ratio. This will ensure that A completely covers B. Otherwise it wont unless they are the same aspect ratio.
rawd
Posts: 3
Joined: 2016-12-29T00:10:41-07:00
Authentication code: 1151

Re: Resizing overlay image at the same time as overlaying?

Post by rawd »

In order to have the entire image covered and keep the aspect ratio, couldn't the caret geometry argument be used instead? =)

-resize WxH^

If I haven't entirely misunderstood that will use the first minimum dimension (either width or height) that fits and lets the other dimension adapt, while maintaining the aspect ratio.

http://www.imagemagick.org/script/comma ... p#geometry
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing overlay image at the same time as overlaying?

Post by fmw42 »

The ^ will resize preserving aspect, but the smaller dimension of the image will be matched to the specified arguments and the larger dimension will be larger than specified according to the aspect. Thus you may want to crop your image, but that would then destroy the aspect ratio. There is no way to preserve aspect ratio if the specified arguments aspect ratio do match that of the image, unless you are willing to accept distorting in one dimension or the other. This option is the ! option.
Post Reply