在 ImageMagick 转换中,样本/重采样/缩放/调整大小/自适应调整大小/缩略图操作符有什么区别?

我发现了多种使用 convert改变图像分辨率的方法:

-sample
-resample
-scale
-resize
-adaptive-resize
-thumbnail

有什么区别吗?

如果我需要制作各种尺寸的大图片缩略图与固定长宽比(裁剪需要)-我的最佳选择是什么?

35048 次浏览

resize

-resize 400x300+20+30 (like the -scale and -sample examples below) converts an input to an output image which has the pixel dimensions of 400x300. It also shifts the output by 20 pixels to the right and by 30 pixels to the bottom. Additionally, there are a few more differences to -scale:

  • -resize does support an additional setting of -filter (which should occur on the commandline before using -resize!).

  • -filter determines the exact algorithm to be used for the colors of added pixels in the case of magnification, or for the colors to be used for the remaining pixels when some of their neighbors are removed in case of minification.

  • For a list of supported filters, simply run convert -list filter.

  • -filter point -resize 400x300 creates exactly the same result as -sample 400x300 does, but it runs still a bit slower.

  • If not set alongside (before) -resize, the conversion will silently default to -filter Lanczos which is slower, but which generates a much better quality (because it takes into account the colors of all surrounding pixels for any newly added ones) than -filter point produces (which uses the nearest neighbor rule to determine the color of a newly added pixels).

sample

-sample 400x300 converts an input image to an output images which has the pixel dimensions of 400x300. However, there are a few very important differences to -resize:

  • -sample does not support the additional setting of -filter; if set anyway, then -filter is simply ignored.

  • When magnifying (because the input image size may be smaller than 400x300), pixels are replicated in blocks.

  • When minifying (because the input image size may be larger than 400x300), pixels are sub-sampled with a very simple algorithm: some rows and columns are simply skipped over.

  • The geometry argument to -sample doesn't support any offset part (unlike -resize, which respects offset directives for the output).

  • The output will never have more (different) colors than the input image had; it may have fewer colors though.

  • Therefore -sample is very fast (faster than -resize) -- but output quality is (usually) worse: you can easily get extreme blocking and aliasing effects in the resulting output.

  • One important feature of -sample is that the new image will not contain any new colors, though some colors from the original image may disappear.

resample

-resample uses as parameter the desired XxY resolution, not not the XxY pixel geometry of the target image. The purpose of this operator is to preserve the rendered size of an image: Assume your image measures 4 inches by 3 inches on a device that renders it at 300 DPI. Then asking for a -resample 72 or -resample 72x72 will resize the image so that it measures (again) 4 inches by 3 inches on a 72 DPI device.

  • This operation works only for such images which already have a desired resolution stored in their metadata (not all image formats do support the concept of an image resolution -- JPEG, PNG and TIFF do).

  • If the source image is in a format where internal support for an image resolution is missing, then the (assumed) original resolution of the image must be specified via -density on the command line prior to specifying the -resample resolution.

scale

-scale 400x300 produces the equivalent result (but is faster, because it avoids all the filter processing) as does using -filter box -resize 400x300. It also completely ignores any current -filter setting.

  • When minifying, it changes the image size simply by replacing pixel colors by averaging the respective input pixel colors together.

  • When magnifying, it simply replicates the respective input pixels for the required additional pixels.

adaptive-resize

-adaptive-resize 400x300 does not support (or does ignore, if set) the offset part of a geometry parameter and also ignores the -gravity setting if present.

  • It uses by default data-dependent triangulation when resizing (unless the resize method is overridden by additionally specifying -filter [something]).

thumbnail

-thumbnail works just like -resize does, with a few differences:

  • It is optimized for speed.

  • It also removes any embedded color profiles to reduce the filesize of the thumbnails.


The following answer shows a few (illustrated!) examples of the -resize directive. Each illustration shows a different result, depending on the 'fine details' of the exact resize method: