Combine images with overlay image as a percent of final img

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
phatz

Combine images with overlay image as a percent of final img

Post by phatz »

I am trying (currently with the windows binary command line utility composite.exe) to take a image that I do not know the size of and have an overlay placed on that image as a % of the final image size.

Example:
MainImage.jpg (size in this case 500x500)
OverlayImage.png 128x128
I would like a new image MainImage2.jpg to be 500x500 (size of the first image) - but have an overlay of 20% (so 100x100) in this case.

Example2:
MainImage.jpg (size in this case 400x300)
OverlayImage.png 128x128
I would like a new image MainImage2.jpg to be 400x300 (size of the first image) - but have an overlay of 20% (so 80x60) in this case.

I can get the overlay to work, just not with the percent based resize:
composite.exe -gravity SouthWest overlayimage.png inputimage.jpg outputimage.jpg

Thanks for any help, I have been searching and not found a way to do this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine images with overlay image as a percent of final img

Post by fmw42 »

composite.exe -gravity SouthWest overlayimage.png inputimage.jpg outputimage.jpg
You need to use parenthesis processing.

Windows syntax:
convert.exe inputimage.jpg ( overlayimage.png -resize 20% ) -gravity SouthWest -composite outputimage.jpg

see parenthesis processing http://www.imagemagick.org/Usage/basics/#image_seq

see compose settings at http://www.imagemagick.org/Usage/compose/#compose

and notes on Windows specific differences at http://www.imagemagick.org/Usage/windows/
phatz

Re: Combine images with overlay image as a percent of final img

Post by phatz »

Thank you for the reply.
The output is better, but still not exactly what I am looking for.

Using:
convert.exe inputimage.jpg ( overlay_large.png -resize 20% ) -gravity SouthWest -composite output.jpg

I do get the output to be the same as the original image (500x500 in this case), however the overlay image is not 20% of the final image size, but 20% of whatever size the overlay image was to start.

I am trying to have the overlay image in the corner always be 20% of the final image, no matter the size of the final image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine images with overlay image as a percent of final img

Post by fmw42 »

Sorry I misunderstood. I don't think you can do that without computing variables for the size of the inputimage and then using those variables in the resize. For example in Unix (sorry I am not a Windows user):

ww=`convert inputimage.jpg -format "%[fx:0.2*w]" info:`
hh=`convert inputimage.jpg -format "%[fx:0.2*h]" info:`
convert inputimage.jpg \( overlayimage.png -resize ${ww}x${hh}\! \) -gravity SouthWest -composite outputimage.jpg

see fx calculations at http://www.imagemagick.org/Usage/transform/#fx_escapes

But you need to be careful about distorting your overlay image if you force it to be 20% of both the width and height as the aspect ratio may change depending upon the aspect ratio of the inputimage and the overlayimage

see geometry at http://www.imagemagick.org/script/comma ... p#geometry
phatz

Re: Combine images with overlay image as a percent of final img

Post by phatz »

Thanks for the info.

Looking in to this more it does seem like I need to script it some. I am no a perl expert, but a little digging around and playing I have a single script that works... Now to just make it a little cleaner.

Code: Select all

#!/usr/local/bin/perl
use Image::Magick;
my($image, $mainImage);
$image = Image::Magick->new;
$mainImage = $image->Read('Folder.jpg');
my($imageOver, $over);
$imageOver = Image::Magick->new;
$over = $imageOver->Read('mp3.png');
$h = $image->Get('height') / 5;
$w = $image->Get('width') / 5;
$over = $imageOver->Resize(geometry=>"$hx$w");
$over = $imageOver->Write('test.jpg');
$mainImage = $image->Composite(image=>$imageOver,gravity=>SouthWest);
$mainImage = $image->Write('output.jpg');
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine images with overlay image as a percent of final img

Post by fmw42 »

This is kind of a kludge. It use an inplace variable calculation. But is one long command line.

It resizes the rose: image to be 1/4 the size of the logo: image and places it in the middle of the logo: image

convert logo: \
\( rose: -resize $(convert logo: -format "%[fx:.25*w]x%[fx:.25*h]" info:) \) \
-gravity center -composite logo_rose.png

The above is unix and I don't know how it might need modifying for Windows.
Post Reply