1

I am working with a Pillow library. I have one gif on which I have to paste another image. While loop over every frames of the gif and pasting image on it, gif distorted.

Someone please suggest a proper solution for it.

Thanks.

1. GIF

enter image description here

2. Image

enter image description here

3. Code

from PIL import Image
from io import BytesIO

animated_gif = './img/gif_distort.gif'
transparent_foreground = './img/cat.jpeg'

img = Image.open(animated_gif)
image = Image.open(transparent_foreground)

duration = []
frames = []

for i in range(img.n_frames):
    img.seek(i)
    frame = img.convert('RGBA').copy()
    duration.append(img.info['duration'])
    frame.paste(image)
    frames.append(frame)

# save gif in temp file
temp_gif_path = 'img/output.gif'
frames[0].save(temp_gif_path, format='GIF', save_all=True, append_images=frames[1:], duration=duration, optimise=True)

Result

enter image description here

4
  • 1
    This appears to be a bug in Pillow. If you call getpalette() on each frame, it seems to assume that the palette is the same for all frames whereas the image actually has a different palette for each frame. Commented Oct 14, 2020 at 9:35
  • 1
    I submitted an issue on Github github.com/python-pillow/Pillow/issues/4977 Commented Oct 14, 2020 at 10:38
  • I've created a pull request to fix this - github.com/python-pillow/Pillow/pull/5857 Commented Nov 29, 2021 at 8:01
  • This problem be resolved as of Pillow 9.0.0, released on January 1. Commented Mar 25, 2022 at 22:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.