Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the length of a multi-page TIFF using Python Image Library (PIL)?

I know that the Image.seek() and Image.tell() methods of PIL allow me to go to a particular frame, and list the current frame, respectively. I would like to know how many frames there are total. Is there a function for getting this info? Alternatively, is there a way in python that I can make a loop and catch for the error that occurs if there is no image?

from PIL import Image
videopath = '/Volumes/USB20FD/test.tif'
print "Using PIL to open TIFF"
img = Image.open(videopath)
img.seek(0)  # .seek() method allows browsing multi-page TIFFs, starting with 0
im_sz = [img.tag[0x101][0], img.tag[0x100][0]] 
print "im_sz: ", im_sz
print "current frame: ", img.tell()
print img.size()

In the above code I open a TIFF stack, and access the first frame. I need to know "how deep" the stack goes so I don't get an error in downstream calculations if no image exists.

like image 820
user391339 Avatar asked Jun 18 '15 23:06

user391339


People also ask

How many pages is a TIFF file?

The TIFF/IT specification (ISO 12639) describes a multiple-file format, which can describe a single page per file set. TIFF/IT files are not interchangeable with common TIFF files.

Can a TIFF file have multiple pages?

The drawings are mostly single page tiff images, but the documents may have multiple pages. For an interim fix to the issue, you will have to download a tiff viewer from the app store.

How do I get the size of an image in Python?

open() is used to open the image and then . width and . height property of Image are used to get the height and width of the image.


1 Answers

If you can wait until 1st July 2015, the next release of Pillow (the PIL fork) will allow you to check this using n_frames.

If you can't wait until then, you can copy that implementation,patch your own version, or use the latest dev version.

More info here: https://github.com/python-pillow/Pillow/pull/1261

like image 133
Hugo Avatar answered Sep 30 '22 17:09

Hugo