UlfR
UlfR

Reputation: 4395

How to resize images in the imagelist to the same size

I want to apply an mask to an image with ImageMagick. But since the image and the mask are not the same size I want to stretch/scale the mask to fit the image.

Typically the input image is 2000x500px, but could be 1000x500px or whatever, and the mask is 400x200px.

So if I knew the input image to always be 2000x500px I could do:

# Resize all images in the list to 2000x500
convert inputfile.png mask.png -resize 2000x500\! \
    -compose over -composite \
    outputfile.png

or

# Resize only the mask to 2000x500
convert inputfile.png \
    \( mask.png -resize 2000x500\! \) \
    -compose over -composite \
    outputfile.png

But my problem is I do not know the size of the inputfile! So how do I resize the mask to the same size as the first image in the list?

Upvotes: 0

Views: 200

Answers (1)

GeeMack
GeeMack

Reputation: 5395

With ImageMagick you can resize the mask to match the dimensions of the input image by using the scaling function of "-distort SRT". An example command might look something like this...

convert input.png mask.png \
   -set option:distort:viewport %[fx:u.w]x%[fx:u.h] \
   -distort SRT "0,0 %[fx:u.w/s.w],%[fx:u.h/s.h] 0 0,0" \
   -composite result.png

That sets the viewport to the dimensions of the input image, then scales both images to fit that viewport. The input image size doesn't change, of course, because it is scaled to 1/1.

Upvotes: 2

Related Questions