0

I was wondering if it is possible in Python to create a program that is able to take the number of pages inside .tiff files, then output exactly how many pages it is all together. I'm new to Python, but want to try writing code that can do this. Is this possible? If so could you please point me in the write direction? From my googling, it seems like I would need to use PIL.

I don't think this is possible but... Is there any metadata information that Python can take from a .tiff file and simply add them all together from all files?

Thank you for the help!

crenshaw-dev
  • 7,504
  • 3
  • 45
  • 81
MKatana
  • 159
  • 1
  • 3
  • 13

3 Answers3

2

Try something like this.

from PIL import Image
img = Image.open("picture.tiff")
i = 0                                                                           
while True:
    try:   
        img.seek(i)
    except EOFError:
        break       
    i += 1          
print i      
2

Yes, it is possible.

This answer provides some direction on how to peek at multiple-image TIFF data with Python Imaging Library. Nathan's answer also gives the specifics for that approach.

It's theoretically possible to just look at the files' header data. For that, you'd want to research the binary structure of the TIFF format and probably use Python's built-in struct library to unpack the header data. But that's some pretty advanced stuff.

With either solution, you'll want a loop to go through the TIFF files. This answer will set you on the right path to find the files in a directory and loop through them.

Update:

Here's a rough approximation of a complete solution:

import os
from PIL import Image

count = 0
tiffs_path = "c:\\wherever"

for filename in os.listdir(tiffs_path):
    if filename.endswith(".tiff"):
        img = Image.open(filename)
        while True:
            try:   
                img.seek(count)
            except EOFError:
                break       
            count += 1          

print count  

This assumes all the TIFF files you care about are in c:\wherever.

Endyd
  • 1,249
  • 6
  • 12
crenshaw-dev
  • 7,504
  • 3
  • 45
  • 81
  • Thank you very much for the code, I appreciate it! I tried changing some things around because I was getting syntax errors. I posted it as another comment because I wasn't sure how to put the correct formatting as a comment. – MKatana Sep 26 '17 at 23:46
1

You can use

Blockquote

from PIL import Image testimage = Image.open(filename,"r") print(testimage.n_frames)

Blockquote