0

How does PIL handle seek() function to operate within multiframe .tiff files? I'm trying to extract a piece of information (greyscale pixel values) of various frames in the file, but no matter what I set the seek for, EOFE error is raised. Example code:

from PIL import Image
im = Image.open('example_recording.tif').convert('LA')

width,height = im.size
image_lookup = 0
total=0
for i in range(0,width):
    for j in range(0,height):
        total += im.getpixel((i,j))[0]

total2=0
im.seek(1)
for i in range(0,width):
    for j in range(0,height):
        total += im.getpixel((i,j))[0]

print total
print total2

The error log looks like this:

File "C:\Users\ltopuser\Anaconda2\lib\site-packages\PIL\Image.py", line 1712, in seek raise EOFError

EOFError

Cheers, JJ

Jericho Jones
  • 157
  • 1
  • 2
  • 11

1 Answers1

1

Was caused by PIL getting to end of the file: can be fixed like this;

class ImageSequence:
def __init__(self, im):
    self.im = im
def __getitem__(self, ix):
    try:
        if ix:
            self.im.seek(ix)
        return self.im
    except EOFError:
        raise IndexError # this is the end of sequence

for frame in ImageSequence(im):
for i in range(0,width):
    for j in range(0,height):
        total += im.getpixel((i,j))[0]
Jericho Jones
  • 157
  • 1
  • 2
  • 11