Home > Net >  Using ImageMagick, how can I resize an image to have minimum height or width, which ever is reached
Using ImageMagick, how can I resize an image to have minimum height or width, which ever is reached

Time:07-08

https://imagemagick.org/Usage/resize/

I do not quite understand how I can resize images to have either a minimum of for example 1000px height OR 1000px width, whichever is reached first, but at the same time do not change images that are a smaller size to begin with as well as keeping the aspect ratio and not filling or cropping the image.

Some examples of what I am trying to achieve:

2000 x 1500 -> 1500 x 1000 (reduced so width fits)
1500 x 2000 -> 1000 x 1500 (reduced so height fits)
2000 x 2000 -> 1000 x 1000 (reduced so both fit)
1100 x 1000 -> 1100 x 1000 (do not change since one measure already fits)
1000 x 1100 -> 1000 x 1100 (do not change since one measure already fits)
1000 x  900 -> 1000 x  900 (do not change since one measure already fits)
 900 x 1000 ->  900 x 1000 (do not change since one measure already fits)
 600 x  500 ->  600 x  500 (do not change since image is smaller)

What I have tried:
Fill Area Flag ('^' flag) Only Shrink Larger Images ('>' flag)

Using an example image of 1842 x 1596

magick in.png -resize 1000x1000^> out.png

But this results in an image that is 1000 x 866 while the result I am looking for would be 1154 x 1000.

CodePudding user response:

Using the comment before i arrived at

magick.exe in.png -resize "%[fx:min(w,h)<=1000 ? w : ( w>h ? (w/h*1000) : 1000) ]" out.png

Give it a shot and tell me if it works for all your usecases

CodePudding user response:

I have not fully tested this, but try the following in Imagemagick 7 (only).

magick in.png -resize "%[fx:max(w,h)>1000?1000:max(w,h)]" out.png
  • Related