Skip to content

Trying to merge gif and image file but getting unexpected output... Plz help #4920

@Pratham31

Description

@Pratham31

What did you do?

Actually I have two files. One is a Transparent gif and the second one is a JPG image and my aim is to use image as a background and gif as a foreground. In short, I want to merge them.

What did you expect to happen?

I want an image as a background and gif as a foreground. In short, I want to merge them.

What actually happened?

But using the pillow library is giving me output like this.
output

What are your OS, Python, and Pillow versions?

  • OS: Windows
  • Python: Python 3.8.5
  • Pillow:
from PIL import Image, ImageSequence
transparent_foreground = Image.open("C:\\Users\\PRATHAMESH\\Desktop\\Python try\\me.jpg")
animated_gif = Image.open("C:\\Users\\PRATHAMESH\\Desktop\\Python try\\celeb (2).gif")

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions