Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize with ImageMagick with a maximal width / height

Tags:

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?

like image 378
Basj Avatar asked Oct 12 '16 20:10

Basj


1 Answers

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.

like image 76
Mark Setchell Avatar answered Sep 28 '22 18:09

Mark Setchell