Python PIL For Loop to work with Multi-image TIFF

1478963 picture 1478963 · Sep 4, 2013 · Viewed 19.8k times · Source

Each tiff file has 4 images in it. I do not wish to extract and save them if possible, I would just like to use a for loop to look at each of them. (Like look at the pixel [0,0] )and depending on what color it is in all 4 I will do something accordingly.

Is this possible using PIL? If not what should I use.

Answer

MatthieuW picture MatthieuW · Sep 6, 2013

You can use the "seek" method of a PIL image to have access to the different pages of a tif (or frames of an animated gif).

from PIL import Image

img = Image.open('multipage.tif')

for i in range(4):
    try:
        img.seek(i)
        print img.getpixel( (0, 0))
    except EOFError:
        # Not enough frames in img
        break