I'm trying to display a gif on top of an image. The gif originally has a transparent background. The code below does that, except it created a black background around the original gif that flickers white when the gif resets. If I don't resize the gif then there would still be a white background around the original gif after its merged with the image.
Is there a way to keep the original gif's background transparent when merging?
from PIL import Image
background = Image.open('GrassyField.png').convert("RGBA")
gif = Image.open("1.gif")
foreground_w, foreground_w = gif.size
background_w, background_h = background.size
frames = []
for num in range(gif.n_frames):
gif.seek(num)
layer = Image.new('RGBA', (foreground_w, foreground_w), (0, 0, 0, 0)).resize((background_w,
background_h))
layer.paste(background, (0, 0), mask=background)
layer.paste(gif.resize((100, 100)).convert('RGBA'), (220, 25))
frames.append(layer)
frames[0].save('output.gif',
save_all=True,
append_images=frames[1:],
duration=100,loop=0)