Lucas
Lucas

Reputation: 75

How to merge a image and a Gif in Python Pillow

I got a partly transparent Image and a GIF.

I want to paste the Image on the GIF with PIL that I get an animated GIF as background with the static image in the foreground.

Upvotes: 1

Views: 6091

Answers (1)

radarhere
radarhere

Reputation: 1039

You may have to adjust this for your own specific images, but here is a starting point -

from PIL import Image, ImageSequence
transparent_foreground = Image.open(...)
animated_gif = Image.open(...)

frames = []
for frame in ImageSequence.Iterator(animated_gif):
    frame = frame.copy()
    frame.paste(transparent_foreground, mask=transparent_foreground)
    frames.append(frame)
frames[0].save('output.gif', save_all=True, append_images=frames[1:])

Upvotes: 8

Related Questions