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.

  • 1
    I saw javascript that did this with a two pass scale followed by a crop at one point. Dec 11, 2011 at 5:54
  • 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. Dec 11, 2011 at 9:30
  • Any chance of writing a bash script? Dec 12, 2011 at 2:04

2 Answers 2


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  

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

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

if AW > BW:  
 crop(A BWxBH)  
 crop(B AWxAH)  
  • 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 Dec 12, 2011 at 22:26
  • 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 Dec 13, 2011 at 7:19
  • 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? Dec 13, 2011 at 7:26
  • (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. Dec 13, 2011 at 17:17
  • 1
    okay 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... Dec 13, 2011 at 17:20

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) \

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.