-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
GIF frames loses transparency with Image.alpha_composite() #4650
Comments
try to add "transparency=0" in all_frames[0].save |
Testing, I find that with #5333, the following code works. all_frames = []
for i in range(frames.n_frames):
frames.seek(i)
all_frames.append(frames)
all_frames[0].save(output_gif, save_all=True, optimize=True, append_images=all_frames[:], loop=0, disposal=2) That is, if I'm correct in thinking that you're just trying to recreate the GIF. I presume you threw in |
@radarhere, a problem with your code above. I find the output GIF has 2256 (=47*48) frames. The original dragon has 47 frames. The output GIF has blown up to almost 14 MB (or 6 MB with my GIF LZW pull #5291). Seems each element of I tried changing On another note, I like the progress made with #5333. I am also trying to fix GifImagePlugin problems with animated GIFs. If you are working on other GIF animation issues I would like to avoid conflicts or duplicated effort. Not sure how to proceed. Also hoping someone is trying out my GIF LZW pull #5291 (@wiredfool ?) and maybe it can get merged someday? I am new to this process. (My #5291 also needs a small change to its Gif.h to add the |
You're right, there are two mistakes in the code I posted. from PIL import Image
frames = Image.open('in.gif')
output_gif = 'out.gif'
all_frames = []
for i in range(frames.n_frames):
frames.seek(i)
disp_frame = frames.copy()
all_frames.append(disp_frame)
all_frames[0].save(output_gif, save_all=True, optimize=True, append_images=all_frames[1:], loop=0, disposal=2) I do not know what you mean by 'a weird GIF' though. I wouldn't worry too much about duplicating efforts. Credit for #5333 should go to the author of #3434, so don't mistake my PR for a grand attempt on my part. If you're interested, and as you may have figured out yourself, I think the ultimate way forward for GIFs is to stop reading in images as P and start reading them in as RGB (for more detail, see #3735). |
@radarhere, While working on the fixes I suggested to your sample code, I somehow managed to comment out the frames.seek(i), and that gave me the weird GIF. I did know that the idea came from zewt in #3434, and it seems clever to me. I would not have thought of handling transparent pixels by just not reading them in. I've been thinking about the entire thing of how to handle animated GIFs and it is a puzzler. Not sure about reading as RGB as you mention in #3735. Solves some problems and causes some problems, it seems. After you get > 256 colors, you need to quantize. Do you also need to dither? Is it best to try to recreate what a frame looks like as it appears when the animation runs? Will those images best "re-compose" into a proper animated GIF? Lotta questions... |
What did you do?
I am trying to remake a transparent GIF by loading a GIF, extracting the GIF and appending them to make a GIF again.
What did you expect to happen?
The result would retain the transparency.
What actually happened?
The frames loses its transparency when using alpha_composite.
What are your OS, Python and Pillow versions?
GIF file:
The text was updated successfully, but these errors were encountered: