Create two sizes of an image using one ImageMagick command

2.3k views Asked by At

I am using ImageMagick to generate small size JPEG versions of large TIFF images. For each TIFF image I must generate two smaller JPEG versions.

I am currently using two convert commands:

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

Converting TIFF to JPEG one-by-one is taking too much time. This approach is inefficient as each image is loaded across the network and processed twice. It will get worse as I plan to create more sizes for each TIFF (think 10,000 TIFFs x 5 sizes).

So, is it possible to generate two or more output files of different sizes using a single ImageMagick command?

2

There are 2 answers

2
Glenn Randers-Pehrson On BEST ANSWER

Yes, it is possible, using the -write option:

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

which rescales the input image to 1600 pixels wide, writes it out, then rescales the result to 1024 pixels wide and writes that. It's important to write the images in descending order of size, to avoid loss of quality due to scaling to a small size and then back up to a larger one.

If you prefer to rescale both images from the input image, use the +clone option:

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

In this case the order of writing images does not matter.

0
Salman A On

Here is an alternate command that uses 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

When tested:

  • the files generated with this command were identical to those created by separate convert commands
  • this command was twice as fast compared to separate commands

Note: ^ is the line continuation character on Windows.