Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I saw javascript that did this with a two pass scale followed by a crop at one point. – RobotHumans Dec 11 '11 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. – peter karasev Dec 11 '11 at 9:30
Any chance of writing a bash script? – Camilo Martin Dec 12 '11 at 2:04

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)  
share|improve this answer
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 Dec 12 '11 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 – RobotHumans Dec 13 '11 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? – RobotHumans Dec 13 '11 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. – peter karasev Dec 13 '11 at 17:17
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... – RobotHumans Dec 13 '11 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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