Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Exception using "seek" on GIF that contains frames with different modes

Open seidnerj opened this issue 3 years ago • 7 comments

I have an animated GIF that has about 60 frames, some of the frames are encoded in 'L' mode and others are in 'RGB' mode. Many actions, including a simple "seek" command" fail with a "ValueError: images do not match" exception when self.im.paste(frame_im, self.dispose_extent) is issued in GifImagePlugin.py. This happens when "self.im" and "frame_im" have different modes.

This might be a protection technique for not being able to process such files with Pillow.

seidnerj avatar Sep 12 '22 14:09 seidnerj

I implemented a small hardcoded fix, though am not sure its the best way to solve this issue, it works for me.

I added the following code (in Pillow 9.2.0):

        if self.im.mode != frame_im.mode:
            if self.im.bands > frame_im.bands:
                frame_im = frame_im.convert(self.im.mode)
            else:
                self.im = self.im.convert(frame_im.mode)

Immediately before the following block in "GifImagePlugin.py":

        if frame_im.mode == "RGBA":
            self.im.paste(frame_im, self.dispose_extent, frame_im)
        else:
            self.im.paste(frame_im, self.dispose_extent)

Either way, the current behavior seems like a bug, even though this might not adhere to animated GIF's spec, it's probably worth a fix.

seidnerj avatar Sep 12 '22 15:09 seidnerj

Could you upload a copy of the image, and simple code that triggers the error?

radarhere avatar Sep 13 '22 01:09 radarhere

Here you go, use the code below and the attached GIF.

def main():
    image = Image.open('example.gif')
    image.seek(6)

if __name__ == '__main__':
    main()

example

seidnerj avatar Sep 13 '22 12:09 seidnerj

I've created PR #6576 to resolve this.

radarhere avatar Sep 13 '22 14:09 radarhere

Great, thanks. I’ll patch my local version accordingly. What release do you expect this to be included in?

seidnerj avatar Sep 13 '22 14:09 seidnerj

Pillow 9.3.0 was due for release on 2022-10-15, but will be delayed a few days due to the delay with the Python 3.11 release. The current estimate is 2022-10-25, you can watch #6460 for updates.

nulano avatar Sep 13 '22 18:09 nulano

Great. Thanks!

seidnerj avatar Sep 13 '22 18:09 seidnerj