在ImageMagick中,如何缩小图像以使其裁剪为特定尺寸? [英] In ImageMagick, how can I scale an image down just enough so it's cropped to particular dimensions?

查看:913
本文介绍了在ImageMagick中,如何缩小图像以使其裁剪为特定尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有1600x1200的大图像,但我想生成250x200(高度和宽度的不同比例)的缩略图 - 我如何将其缩小到大小然后裁剪它?这可以在一行中使用转换吗?

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?

推荐答案

缩小+ 填充给定区域(并伸展输出图像,如果需要)



如果你想缩小到250x200像素(不保持纵横比),完全填充250x200的空间(拉伸/扭曲图像,如果必要的),使用:

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

(注意 \\!!缩放几何设置后的字符!)

替换 .jpeg .png 后缀包含您拥有和想要的任何格式后缀,ImageMagick将自动处理它。

(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.

如果要缩小到内部 250x200像素,同时保持原始图像的纵横比(高度与宽度之比),裁剪那么250x200像素的尺寸没用完,使用:

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

默认宽度和高度在<$ c中给出$ c> -resize 参数是最大值(除非您将其指定为百分比)。这意味着:输出图像展开或收缩以适合指定的尺寸,从而保持图像的纵横比。

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.

上面的这个命令尝试将尺寸设置为250x200:

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


  • 当高度从原始的1200像素缩小到200像素(到此原始值的16.6666%)时,宽度现在为266.66像素。所以它还不适合....

  • 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....

当宽度缩小到原始1600像素的250像素时(相当于原始值的15.625%) )现在的高度是187.5像素。所以它现在适合,并且可以在顶部和底部留出一些空间。

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.

所以最后的尺寸将是可能是250x187或更高,250x188像素(你不能在高度上有一个像素的分数,它需要一个整数!)。

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!).

如果你想要缩小到内部 250x200像素,保持原始的宽高比并创建一个全尺寸250x200像素的图像,通过添加一些黄色背景使该空间中的真实图像居中,使用:

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

将背景颜色替换为您喜欢的任何其他颜色更好,或者用于透明背景(不支持JPEG输出!)。

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

如果你想缩放到250x200像素保持方面比率,完全填充250x200的空间(但没有裁掉任何溢出),使用:

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

(注意缩放几何设置后的 ^ 字符。 ^ 功能需要最小的ImageMagick版本6.3.8-2。)

使用<$ c修改宽度和高度时$ c> ^ 除了 -scale -resize 参数外,这些设置是被认为是最小值,其中只有其中一个尺寸确实达到了。这意味着:输出图像展开或收缩,直到宽度或高度与其指定尺寸匹配,从而保持图像的纵横比。

(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.

上面的这个命令尝试将尺寸设置为250x200:

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


  • 当高度从原始的1200像素缩小到200像素(到此原始值的16.6666%)时,实现了所需的缩放。宽度现在为266.66像素。这四舍五入为267像素。

  • 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.

所以最终尺寸为267x200像素。

So the final dimension will be 267x200 pixels.

如果要保持纵横比缩放到250x200像素,完全填充250x200的空间(从左右边缘裁剪条带),请使用:

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

图像的最终尺寸为250x200。与上一个命令(给出250x267结果)相比,左边和右边的一些像素列被删除。

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.

这篇关于在ImageMagick中,如何缩小图像以使其裁剪为特定尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆