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
2. Image
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



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.