Creating and splitting large multipage TIFF images

I had the same problem today while trying to split a 1700 image, 1G tif file. 16G of memory wasn't enough, then tried having it cache on disk, but that was slow and it easily exhausted more than 100G on the hard drive without accomplishing anything (this was probably a bug).

But apparently ImageMagick can extract a specific tif from the original file without loading it completely, so was able to split the bigger file with a simple bash script:

subfiles=$(identify -quiet -format '%n\n' largefile.tif | head -n1)
for (( i = 0; i < subfiles; i++ )); do
    convert largefile.tif[$i] -scene 1 split/smallerfile_$i.tif
done

No idea though how to create a big file without running out of memory, so maybe this is half an answer?


I find @tarikki's answer one of the best, because it really doesn't hang the server nor does it eat RAM and disk space, and it's fast.

Some improvements that helped me:

  1. replace END=2000 by END=$(identify -format "%n" largefile.tif)
  2. The TIF index is 0-based, so the looping should start at 0 and use < instead of <= : for((i=0;i<END;i++))