Exception using "seek" on GIF that contains frames with different modes
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.
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.
Could you upload a copy of the image, and simple code that triggers the error?
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()

I've created PR #6576 to resolve this.
Great, thanks. I’ll patch my local version accordingly. What release do you expect this to be included in?
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.
Great. Thanks!