'Background is white instead of being transparent

What did you do?

Overlay add png images on a gif file frames What did you expect to happen?

Get a gif with the pngs added on each layer and a transparent background What actually happened?

I'm getting the gif as I want it but with a white background What are your OS, Python and Pillow versions?

OS: MacOs 11.6.5
Python: python3.7
Pillow: 9.1

All the png files have a transparent background and also the gif has a transparent background !

Here is an image from the end result gif (the file is too big i couldn't upload it here): enter image description here

from PIL import Image, ImageSequence

background   = Image.open('images/1.png')#.convert('RGBA')
animated_gif = Image.open("images/2.gif")
layer1 = Image.open('images/3.png')
layer2 = Image.open('images/4.png')
all_frames = []


for gif_frame in ImageSequence.Iterator(animated_gif):


    new_frame = background.copy()
    layerr1 = layer1.copy()
    layerr2 = layer2.copy()

    gif_frame = gif_frame.convert('RGBA')
    
    new_frame.paste(layerr2, mask=layerr2)
    new_frame.paste(gif_frame, mask=gif_frame) 
    new_frame.paste(layerr1, mask=layerr1)
    all_frames.append(new_frame)


all_frames[0].save("image2.gif",transparency = 255, background = 255, save_all=True, append_images=all_frames[1:], duration=50, loop=0)

Please help me with this, this is the first time I'm using pillow !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source