使用一个 ImageMagick 命令创建两种尺寸的图像

Create two sizes of an image using one ImageMagick command

我正在使用 ImageMagick 生成大型 TIFF 图像的小型 JPEG 版本。对于每个 TIFF 图像,我必须生成两个较小的 JPEG 版本。

我目前正在使用两个 convert 命令:

convert.exe 4096-by-3072px-120mb.tif -resize "1024x>" -strip -interlace Plane 1024px-wide-for-web.jpg
convert.exe 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane 1600px-wide-for-web.jpg

将 TIFF 一个一个地转换为 JPEG 花费了太多时间。这种方法效率低下,因为每个图像都通过网络加载并处理两次。它会变得更糟,因为我计划为每个 TIFF 创建更多尺寸(想想 10,000 个 TIFF x 5 尺寸)。

那么,是否可以使用单个 ImageMagick 命令生成两个或多个不同大小的输出文件?

是的,可以使用 -write 选项:

convert 4096-by-3072px-120mb.tif -resize "1600x>" -strip -interlace Plane \
-write 1600px-wide-for-web.jpg -resize "1024x>" 1024px-wide-for-web.jpg

将输入图像重新缩放为 1600 像素宽,将其写出,然后将结果重新缩放为 1024 像素宽并写入。重要的是按大小的降序写入图像,以避免由于缩放到小尺寸然后再返回到更大尺寸而导致质量下降。

如果您希望从输入图像重新缩放两个图像,请使用 +clone 选项:

convert 4096-by-3072px-120mb.tif -strip -interlace Plane \
 \( +clone -resize "1024x>" -write 1024px-wide-for-web.jpg +delete \) \
  -resize "1600x>" 1600px-wide-for-web.jpg

在这种情况下,写入图像的顺序无关紧要。

这是一个使用 memory program register 的替代命令:

magick.exe 4096-by-3072px-120mb.tif -write mpr:main +delete ^
mpr:main -resize "1024x>" -quality 80 -interlace Plane -strip -write 1024px-wide-for-web.jpg +delete ^
mpr:main -resize "1280x>" -quality 80 -interlace Plane -strip -write 1280px-wide-for-web.jpg +delete ^
mpr:main -resize "1600x>" -quality 80 -interlace Plane -strip -write 1600px-wide-for-web.jpg +delete ^
mpr:main -resize "2048x>" -quality 80 -interlace Plane -strip        2048px-wide-for-web.jpg

测试时:

  • 使用此命令生成的文件与通过单独的转换命令创建的文件相同
  • 这个命令比单独的命令快两倍

注:^是Windows上的续行符。