Any way to make two images the same size and/or aspect ratio in ImageMagick?

1

Suppose I want to have two images side by side in a slide but they are different sizes. I know I can use "mogrify" and "convert" and all that to resize them, but it's a pain to type in the exact parameters.

Is there some tool or script (and matlab would be fine, for example) that: resizes images to make same aspect ratio, and maximally preserve image content?

E.g. let W x H be the naming convention. A is 400x300 and B is 403x299. So we then will make A => 400x299 and B => 400x299; same size and maximal area is preserved in both images.

peter karasev

Posted 2011-12-11T03:24:29.217

Reputation: 193

1I saw javascript that did this with a two pass scale followed by a crop at one point. – RobotHumans – 2011-12-11T05:54:33.220

hm that could work, but do you know what exactly the code is, and is it even straightforward to run javascript from shell? I'm thinking some logic wrapping imagemagick would do it... depending how ambitious I am, I might write such a function perhaps ... it is extremely annoying to see mis-sized images on printed paper or projector. – peter karasev – 2011-12-11T09:30:25.797

Any chance of writing a bash script? – Camilo Martin – 2011-12-12T02:04:07.880

Answers

1

if you're running it from a shell the python imagemagick bindings would probably do it. the logic would be something like:

AW x AH  
BW x BH

if AW > BW:  
 scale(A BW/AW)  
else:
 scale(B AW/BW)  

if AH > BH:  
 scale(a BH/AH)  
else:  
 scale(B AH/BH)  

if AW > BW:  
 crop(A BWxBH)  
else:  
 crop(B AWxAH)  

RobotHumans

Posted 2011-12-11T03:24:29.217

Reputation: 5 758

that could work I guess, but this is only cropping, you might have a 400x300 and 720x445 image so need to scale+crop in a nontrivial way... tempted to write c++/opencv for this in lieu of existing app – peter karasev – 2011-12-12T22:26:43.000

it scales the larger image to the smaller size, then crops off the remaining bit. you would need to re-init the w and h values – RobotHumans – 2011-12-13T07:19:58.730

do you mean that you need an additional chunk of logic that determines which distortion is larger? like if BW/AW > BH/AH scale BW/AW else scale BH/AH crop? – RobotHumans – 2011-12-13T07:26:13.477

(1) you mention one issue. (2) more immediate, is the "AW x AH" actual syntax? How do you load in the file attributes? (3) there are other issues, such as cropping first being better due to less resolution lost, but then the crop needs to be divisible by the right factor for scaling. But this is 'nice to have', not necessary. – peter karasev – 2011-12-13T17:17:03.350

1okay so i'm not here to write scripts for people so no it's not the actual syntax. cropping first would not lead to any difference in resolution unless you lost more image space. that's a choice. image space or resolution. think i'm done on this question... – RobotHumans – 2011-12-13T17:20:48.557

yeah totally, really it should be either an existing tool that "just works" or I'd have to write something like your logic in a language that I know well enough, which unfortunately isn't python :-/ – peter karasev – 2011-12-14T07:19:27.913

0

ImageMagick's convert command can do that in one go. But you must spezify the resize parameter by executing an identify command within the other one. :-)

Resize one image to match the size of another one

 convert \
    to-be-resized.orig.img \
   -resize $(identify \
                -ping \
                -format "%wx%h\!" \
                 master.img) \
    orig-is-resized.jpg

Update: Oh, I just notice that my command doesn't do exactly what you wanted. But almost. :-) It should still give you the basic idea how you could handle the problem. Should be easy to translate that into a shell script that does exactly what you want.

Kurt Pfeifle

Posted 2011-12-11T03:24:29.217

Reputation: 10 024