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.
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