Is there a way to get the number of tiffs in a multi-tiff with a command line utility in ImageMagick?

What I want to to is to extract (split) the multi-tiff into single files. However if the tiff only contains one tiff (is not a multi-tiff), I would like to do nothing at all... Also if I use the "%d"-option to name the output files I get the "%d" in the file name if it is only one.

...or is there another way to use ImageMagick to avoid strange filenames when converting?


You can return the number of pages in a tiff using identify -format "%p" multipage.tif.

| improve this answer | |
  • 1
    Interesting! Tried it and got back: 12345678910111213141516171819202122 if my tiff holds 22 images. Not quite what i expected but will investigate more... – Anders Apr 15 '10 at 7:06
  • Hmmm..... I don't have a multipage tiff to try it on. What version of IM are you using? On what OS? – DaveParillo Apr 15 '10 at 16:32
  • I have tried 32-bit IM version 6.6.1-2 Q8 and Q16 on Windows XP 32-bit and Windows 7 64-bit. I have also tried 64-bit IM 6.6.1-2 on Windows 7 64-bit. All generate the same output with the command you suggested. – Anders Apr 16 '10 at 6:43
  • 3
    I found the documentation for the strings in the format option (available at imagemagick.org). %p displays pages and %n displays the number of scenes, which is what I was looking for. The command I will use is then: identify -format "%n" multipage.tif . – Anders Apr 16 '10 at 6:56

Shorter than all of the others:

identify multipage.tif | wc -l
| improve this answer | |
  • Works for me... even when none of the -format options produce any output. It should be noted that the reason this works even when identify spews lots of error messages (without |wc -l), is because errors are written to stderr, and wc only reads stdout – Lambart May 3 '17 at 18:04
  • @Lambart: wc reads stdin, not stdout. It is the pipe that connects stdout from the former to stdin of the latter. There are other pipes available as well. – Robert Siemer Oct 31 '17 at 18:24
  • @RobertSiemer well-clarified, thanks – Lambart Oct 31 '17 at 23:09

You can use the following to count the number of pages in a TIFF file:

tiffinfo output_file_name.tif | grep "Page Number" | grep -c "P"
| improve this answer | |
  • tiffinfo is a new one to me. Interesting utility, but it isn't able to detect any second image in the tiff I'm working with... also, for a 80MB TIFF, it's generating 345MB of output. – Lambart May 3 '17 at 18:02

identify multipage.tif | wc -l

(-format "%p" or %n produced bogus results for me)

| improve this answer | |

Combined from the answers supplied (using -format and wc to count):

identify -format "%p " multipage.tif | wc -w

And just to mention ... it's the page numbers we get ( 1 for 1 page, 1 2 for 2 pages, 1 2 3 for 3 pages and 1 2 3 ... 19 20 21 22 for 22 pages)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.