Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only shrink larger images using ImageMagick to a ratio

Using ImageMagick, I can easily have a screenshot of what I want, but I'd like to resize it for using less space. I found this :

convert screen.jpg    -resize 1280x1024\!  screen.jpg

But I'd like to resize it based on the most bigger size (width OR height) and the other one (height OR width) will be proportionnaly resized too.

For example, say I want all my image to be resized to 600px at their most width/height size :

  • 1920x1200 => width is the biggest : 600x375
  • 600x1200 => height is the biggest : 300x600

How can I do that with ImageMagick? (or at least, defining one max size (only width for example)).

Thanks for your help!

Note: is it possible to implement it automatically with the import command?

like image 639
Cyril N. Avatar asked Jun 17 '11 10:06

Cyril N.


People also ask

How to resize images using ImageMagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new. png.

How do I resize an image in a ratio?

Click Upload an image and select the image you want to crop. Under step 2, click the Fixed Aspect Ratio button, then enter that ratio, such as 5 and 2, and click Change. Drag a rectangle over the image to select the area you want.

How do I resize an image without losing the ratio?

Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.

Which command is used to make an image shorter or longer?

Answer: The "at" symbol ' @ ', will resize an image to contain no more than the given number of pixels. This can be used for example to make a collection of images of all different sizes roughly the same size. For example here we resize both our images to a rough 64x64 size, or 4096 pixels in size.


2 Answers

To preserve aspect ratio:

convert -resize 600x600 screen.jpg:

  • 1920x1200 => 600x375
  • 600x1200 => 300x600
  • 150x300 => 300x600
  • 300x150 => 600x300

convert -resize 600x600\> screen.jpg:

  • 1920x1200 => 600x375
  • 600x1200 => 300x600
  • 150x300 => 150x300 (is not resized to bigger size)
  • 300x150 => 300x150 (is not resized to bigger size)
like image 50
AndreyP Avatar answered Oct 22 '22 22:10

AndreyP


To preserve aspect ratio, you can shrink the image by a certain scale:

convert -resize 50% screen.jpg

Or use a pixel area:

convert -resize 180000@ screen.jpg

This would also blow up small images to the specified area. If you want ImageMagick to shrink your large images but keep small images untouched, use the ">" operator:

convert -resize '180000@>' screen.jpg

Note that you then need to quote the geometry argument in order to prevent your shell interpreting the ">" sign as a file redirectors.

See ImageMagick documentation for these and other options: http://www.imagemagick.org/script/command-line-processing.php#geometry

like image 23
EPSG31468 Avatar answered Oct 22 '22 22:10

EPSG31468