-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to merge gif and image file but getting unexpected output... Plz help #4920
Comments
This is similar to #4917 - except that issue has disappeared. |
A few comments.
Given that, and working around a transparency mask error, I get from PIL import Image, ImageSequence
background = Image.open("me.jpg")
animated_gif = Image.open("celeb (2).gif")
frames = []
for frame in ImageSequence.Iterator(animated_gif):
output = background.copy()
transparent_foreground = frame.convert('RGBA')
output.paste(transparent_foreground, (0, 0), mask=transparent_foreground)
frames.append(output)
frames[0].save('output.gif', save_all=True, append_images=frames[1:]) But there is an issue with the background there. Instead, see what you think of this - it loops through the pixels and copies them individually, ignoring the transparent or background pixels. from PIL import Image, ImageSequence
background = Image.open("me.jpg")
animated_gif = Image.open("celeb (2).gif")
frames = []
for frame in ImageSequence.Iterator(animated_gif):
output = background.copy()
frame_px = frame.load()
output_px = output.load()
transparent_foreground = frame.convert('RGBA')
transparent_foreground_px = transparent_foreground.load()
for x in range(frame.width):
for y in range(frame.height):
if frame_px[x, y] in (frame.info["background"], frame.info["transparency"]):
continue
output_px[x, y] = transparent_foreground_px[x, y]
frames.append(output)
frames[0].save('output.gif', save_all=True, append_images=frames[1:]) |
Thank you, sir. It works 👍🏻 |
But I have one question that It is taking time to save the GIF, right. Why is it so? |
And after some time it is again showing that weird image. |
As is noted in the docs, manipulating individual pixels like I am doing in my second example is slow. The first code example I provided is much faster, but has a problem with the background. With the 'weird image', I presume you're referring to the very last frame of the output GIF? |
Not the best way, but the easiest way to fix that would be to just remove the last frame. from PIL import Image, ImageSequence
background = Image.open("me.jpg")
animated_gif = Image.open("celeb (2).gif")
frames = []
for frame in ImageSequence.Iterator(animated_gif):
output = background.copy()
frame_px = frame.load()
output_px = output.load()
transparent_foreground = frame.convert('RGBA')
transparent_foreground_px = transparent_foreground.load()
for x in range(frame.width):
for y in range(frame.height):
if frame_px[x, y] in (frame.info["background"], frame.info["transparency"]):
continue
output_px[x, y] = transparent_foreground_px[x, y]
frames.append(output)
frames[0].save('output.gif', save_all=True, append_images=frames[1:-1]) |
Can we reduce the file size of GIF? |
Not with Pillow, see #617 (comment) for that. |
And what if I want a gif at the left below corner? |
Any modification in the above removed last frame program ? |
Here is a modified version where I first scale down your GIF to half the size, and then locate it in the lower left corner. from PIL import Image, ImageSequence
background = Image.open("me.jpg")
animated_gif = Image.open("celeb (2).gif")
frames = []
for frame in ImageSequence.Iterator(animated_gif):
output = background.copy()
# Reduce the GIF to half the size
frame = frame.resize((frame.width // 2, frame.height // 2))
frame_px = frame.load()
output_px = output.load()
transparent_foreground = frame.convert('RGBA')
transparent_foreground_px = transparent_foreground.load()
for x in range(frame.width):
for y in range(frame.height):
if frame_px[x, y] in (frame.info["background"], frame.info["transparency"]):
continue
# Offset the y value when setting the pixel
# since this is now the lower left, rather than the upper left
output_px[x, y+output.width/2] = transparent_foreground_px[x, y]
frames.append(output)
frames[0].save('output.gif', save_all=True, append_images=frames[1:-1]) There are some bugs in the way that Pillow handles GIFs. The code I have given you in this issue is not ideal, but works because I presume you are personally more interested in an immediate solution than in a permanent one. |
how can we add multiple gif over a jpg file?? |
I want to add 2 animated gif over a jpg image |
thank you soo much |
Between #5291 in Pillow 8.2.0 and #7568 when using |
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.
What are your OS, Python, and Pillow versions?
The text was updated successfully, but these errors were encountered: