1

I have vertical (i.e. height > width) and horizontal (i.e. width > height) images. I want to resize them to either 900-pixels-height vertical ones or 900-pixels-width ones based on their original ratio.

I am using Ubuntu 20.04 in Gnome environment. I think that mogrify of imagemagick will solve this problem, a pseudo code would be like...

for i in *.jpg; do
  _w = identify -format %w
  _h = identify -format %h
  
  if [_w >= _h]; then
    mogrify -resize 900x675 i
  else
    mogrify -resize 675x900 i
  fi    
done

but couldn't find the way.

2
  • Btw: You should use shellcheck.net before posting to find and fix syntax errors which you have lots of.
    – pLumo
    Feb 28 at 17:58
  • Thank you for pointing-out. That's just a pseudo code to show my purpose. I edited my post to specify this.
    – htlee
    Feb 28 at 18:52
2

This should be enough:

mogrify -resize 900x900\> *.jpg

From https://legacy.imagemagick.org/Usage/resize/#noaspect

Aspect ratio is preserved basically so that any circles in the input image will remain a circle in the output image. That is the image will not be squashed or squeezed, only resized, unless you tell it otherwise.

and

Only Shrink Larger Images ('>' flag) Another commonly used option is to restrict IM so that it will only shrink images to fit into the size given. Never enlarge.

1
  • Thank you! It works.
    – htlee
    Feb 28 at 18:53

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.