Resize with ImageMagick with a maximal width / height

Basj picture Basj · Oct 12, 2016 · Viewed 10.2k times · Source

The command

imageconvert.exe in.jpg -resize 800x600 out.jpg

resizes the image so that it keeps original ratio, with maximum width of 800, and maximum height of 600 pixels. But if the image is smaller both in width and height (e.g. a 300x200 image), it will be enlarged to reach 800 or 600, and I don't want this.

How to keep the same kind of resizing (when width > 800 or height > 600), but such that an image that is smaller both in width and height (e.g. a 300x200 image), will be untouched?

Answer

Mark Setchell picture Mark Setchell · Oct 12, 2016

I think you need the > flag on the resize. Let's create some images (one red 300x200, another blue 1000x500):

convert -size 300x200 xc:red   small.png
convert -size 1000x500 xc:blue large.png

Now convert them both to 800x600 with no flags:

convert small.png -resize 800x600 a.png   # 800x533
convert large.png -resize 800x600 b.png   # 800x400

Now with flags:

convert small.png -resize 800x600\> a.png # 300x200
convert large.png -resize 800x600\> b.png # 800x400

You may need a caret (^) rather than a backslash on Windows.

The various flags are explained in the documentation here. Thanks to @user1133275 for the suggestion.