Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 482
ensnare Avatar asked Aug 31 '12 21:08

ensnare


People also ask

How do I scale an image down?

Step 1: Right-click on the image and select Open. If Preview is not your default image viewer, select Open With followed by Preview instead. Step 2: Select Tools on the menu bar. Step 3: Select Adjust Size on the drop-down menu.

How do I crop an image in ImageMagick?

To crop an image using ImageMagick, you need to specify the X and Y coordinates of the top corner of the crop rectangle and the width and height of the crop rectangle. Use the mogrify command if you want the images to be replaced in-place or use the convert command otherwise to make a copy.

What does it mean to scale down an image?

Image scaling is the process of resizing a digital image. Scaling down an image makes it smaller while scaling up an image makes it larger. Both raster graphics and vector graphics can be scaled, but they produce different results.

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

3. What is the difference between Resize and Skew commands? Answer: Resize allows you to make the image narrower, wider, shorter or longer.


1 Answers

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.

like image 97
Kurt Pfeifle Avatar answered Sep 19 '22 17:09

Kurt Pfeifle