Search code examples
imagemagickwebp

ImageMagick: Splitting an image plus converting to .webp results in 1 image


I'm using a script to split the quad-layout Midjourney output into 4 separate images:

splitName="${filename}-split.png"
convert -crop 50%x50% $input $splitName

This works fine. With an input of image.png, I get:

  • image-split-1.png
  • image-split-2.png
  • image-split-3.png
  • image-split-4.png

I would like to do some lightweight compression on the images by converting them to WebP format. However when I change the output name line like this:

splitName="${filename}-split.webp"

The script runs, but I only get a single image out: image-split.webp. Where are the other 3 images?


Solution

  • I decided to write this up a bit more. In Imagemagick, some formats (such as TIFF, GIF, PDF and apparently WEBP) can create multi-frame files. That is they contain multiple images in one file. Formats such as JPG and PNG do not support that. In the latter, you will will get multiple output images rather than one file containing multiple images.

    So to allow the user to choose for TIFF, GIF, WEBP etc, there is the adjoin operator. -adjoin will combine images into one file. +adjoin will keep them as separate files.

    So in the case above, if

    convert $input -crop 50%x50% +repage $splitName
    

    makes one file (for TIFF, GIF, etc) containing multiple frames/pages/images

    we can do

    convert $input -crop 50%x50% +repage +adjoin $splitName
    

    to keep the output as multiple image files rather than one file containing multiple images.

    The +repage is used to remove the virtual canvas geometry for GIF or PNG or TIFF or other formats that support it. Note that JPG does not support it. The virtual canvas geometry keeps the size of the input before the crop and the upper left corner of the crop as meta data in the output file.