In ImageMagick, how can I scale an image down just enough so it's cropped to particular dimensions?

StackOverflow https://stackoverflow.com/questions/12222839

  •  29-06-2021
  •  | 
  •  

Question

For example, if I have a large image that's 1600x1200, but I want to generate a thumbnail that is 250x200 (a different ratio of height and width) -- how can I scale this down to size and then center crop it? Is this possible in one line using convert?

Was it helpful?

Solution

Scale down + fill given area (and stretch output image if required)

If you want to scale down to 250x200 pixels (without keeping the aspect ratio), completely filling the space of 250x200 (stretching/distorting the image if necessary), use:

  convert            \
     input.jpeg      \
    -scale 250x200\! \
     output1.png

(Note the \! characters after the scale geometry setting!)
Replace the .jpeg and .png suffixes with whatever format suffixes you have and want, ImageMagick will automatically handle it.


Scale down + fit result into give area (and keep aspect ratio, and if required, waive some of the area given)

If you want to scale down to inside 250x200 pixels while keeping aspect ratio (the ratio of height to width) of the original image and cropping that dimension of the 250x200 pixels which isn't used up, use:

  convert          \
     input.jpeg    \
    -scale 250x200 \
     output2.png

By default width and height given in the -resize argument are maximum values (unless you specify it as a percentage). This means: output images expand or contract to fit the specified dimensions, maintaining the aspect ratio of the image.

This command above "tries" to set the dimensions to 250x200:

  • When the height is shrunk to 200 pixels from the original's 1200 pixels (to 16.6666% of this original value), the width is now 266.66 pixels. So it doesn't fit yet....

  • When the width has shrunk to 250 pixels from the original 1600 pixels (to 15.625% of this original value) the height now is 187.5 pixels. So it fits now, and can leave over some space at the top and bottom.

So the final dimension will be either of 250x187 or more likely, 250x188 pixels (you can't have fractions of a pixel in the height, it requires an integer!).


Scale down + center result in given area (and use some background color to fill remaining space)

If you want to scale down to inside 250x200 pixels, keeping aspect ratio of the original and creating an image that is the full-sized 250x200 pixels centering the real image in that space by adding some yellow background, use:

  convert              \
     input.jpeg        \
    -scale 250x200     \
    -gravity center    \
    -background yellow \
    -extent 250x200    \
     output3.png

Replace the background color with any other you like better, or with none for transparent background (not supported for JPEG output!).


Scale down keeping aspect ratio + without cropping to given area

If you want to scale to 250x200 pixels keeping the aspect ratio, completely filling the space of 250x200 (but not cropping off any overflowing), use:

  convert             \
     input.jpeg       \
    -scale '250x200^' \
     output4.png

(Note the ^ character after the scale geometry setting. The ^ feature requires a minimum ImageMagick version of 6.3.8-2.)
When modifying width and height with the ^ addition to the -scale or -resize arguments, these settings are considered to be minimum values where only one of these too dimensions is really attained. This means: output images expand or contract until either width or height matches its specified dimension, maintaining the aspect ratio of the image.

This command above "tries" to set the dimensions to 250x200:

  • When the height is shrunk to 200 pixels from the original's 1200 pixels (to 16.6666% of this original value), the wanted scaling is achieved. Width is now 266.66 pixels. This is rounded to 267 pixels.

  • So the final dimension will be 267x200 pixels.


Scale down keeping aspect ratio + with cropping to given area

If you want to scale to 250x200 pixels keeping the aspect ratio, completely filling the space of 250x200 (cropping off strips from left and right edges), use:

  convert             \
     input.jpeg       \
    -scale '250x200^' \
    -gravity center   \
    -extent 250x250   \
     output5.png

The final dimension of the image will be 250x200. Compared to the previous command (which gave a 250x267 result), some pixel columns from the left and right edges are removed.

OTHER TIPS

Accepted answer doesn't do what question asks.

If you need exact output dimension (for example 250x200), fit maximum information from the original image ang crop redundant parts to keep aspect ratio, use this command:

convert input.jpg -scale "250x200^" -gravity center -crop 250x200+0+0 output.jpg

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top