Imagemagick 转换调整大小然后裁剪

Imagemagick convert resize then crop


我有超过 1000 张不同分辨率的图片(例如 1234x2122、4400x5212 等),我想将它们全部转换为固定的 100x100 大小,所以。

  1. 首先我需要调整图像的大小以保持比例,并得到 100xA 或 Ax100,其中 A > 100(这取决于图像的宽度和高度,对于某些图像宽度 > 高度,对于某些图像高度 > 宽度)。

  2. 将此图片从中心裁剪为 100x100

是否有一个简单的 convert 命令,我可以将其用于我的所有图像?

您将使用 area-fill (^) geometry modifier on the -resize operation to unify the down-scale. For cropping the center, -extent with -gravity Center 即可。

convert input.jpg -resize 100x100^ \
                  -gravity Center  \
                  -extent 100x100  \
        output.jpg

更新

正如 Mark Setchell 在评论中指出的那样,mogrify 实用程序可用于批量转换项目。

mogrify -path ./path/to/write/results/ \
        -resize 100x100^ \
        -gravity Center  \
        -extent 100x100  \
        ./path/to/source/files/*

Reminder: Mogrify will overwrite original file with resulting image(s), unless you set the -path parameter.

为了保持宽高比并且不填充任何东西(范围),而是从长边裁剪,这超出了你可以做的

convert input.jpg -resize 100x100^ \
                  -gravity Center  \
                  -crop 100x100+0+0 +repage  \
        output.jpg

如果您愿意,可以选择从一侧裁剪更多