Skip to content
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

Black background on frames after first frame in GIF using Pillow #5755

Closed
AnasYasin opened this issue Oct 11, 2021 · 8 comments
Closed

Black background on frames after first frame in GIF using Pillow #5755

AnasYasin opened this issue Oct 11, 2021 · 8 comments
Labels

Comments

@AnasYasin
Copy link

AnasYasin commented Oct 11, 2021

What did you do?

I am trying to place a gif on png using Pillow. It's giving black background after the first frame. I have tried every solution on Github including #1987 and #1976

What did you expect to happen?

It should make a transparent GIF place on a png.

What actually happened?

Currently, all frames after the first frame are having a black background. The code is working file on small GIFs.

Orignal Gif:

Background Image:

Final Result:

Frames:


What are your OS, Python and Pillow versions?

  • OS: Linux Mint 19.1 x86_64
  • Python: 3.6.8
  • Pillow: 8.2.0
from PIL import Image, ImageSequence

gif = Image.open("gif4.GIF")
duration = 100
disposal = 2
frames = []
ind = 0

for frame in ImageSequence.Iterator(gif):
    bg2 = Image.open('test1.png')
    bg2.paste(frame, frame.convert('RGBA'))
    bg2.save('{}.png'.format(ind))
    frames.append(bg2)
    ind  += 1
    
frames[0].save('test.gif', save_all=True, append_images=frames[1:], loop=0, optimise=False, duration=duration, disposal=disposal, format='GIF')
@radarhere radarhere added the GIF label Oct 11, 2021
@radarhere
Copy link
Member

If you simplify your code to just reading and saving your GIF,

from PIL import Image, ImageSequence

gif = Image.open("gif4.GIF")
ind = 0

for frame in ImageSequence.Iterator(gif):
    frame.save('{}.png'.format(ind))
    ind  += 1

you will see that the colours are still not correct. I expect this is part of a long standing bug where Pillow doesn't correctly read the colours from different palettes after the first GIF frame. See #3735 for a longer explanation.

@AnasYasin
Copy link
Author

you will see that the colours are still not correct. I expect this is part of a long standing bug where Pillow doesn't correctly read the colours from different palettes after the first GIF frame. See #3735 for a longer explanation.

Yes, I am stuck on this for a week now. I on read an issue that this bug has been resolved in 8.1 but I'm still having this problem. Can you suggest any other library of way-out to get this done in Python?
Thanks.

@radarhere
Copy link
Member

Good news: I was wrong. This was not our long standing bug, but is instead the same as #5656, fixed by #5756. Your code will work correctly in Pillow 8.4.0.

Extra good news: Pillow 8.4.0 is due to be released in two days.

@radarhere
Copy link
Member

Pillow 8.4.0 has been released on PyPI. So if you upgrade, this should now work for you.

@tomhaydn
Copy link

I am having this issue on 8.4.0

@tomhaydn
Copy link

tomhaydn commented Nov 16, 2021

from PIL import Image, ImageSequence, GifImagePlugin
import os


bg = Image.open('images/bg/Background Sky.gif')
body = Image.open('images/body/Body Blue.gif')
eyes = Image.open('images/eyes/Eyes Green.gif')
headwear = Image.open('images/headwear/Headwear Party Hat.gif')

frames = []
for idx, frame in enumerate(ImageSequence.Iterator(bg)):
    #frame.paste(ImageSequence.Iterator(bg)[idx], mask=ImageSequence.Iterator(bg)[idx].convert("RGB"))
    frame = frame.copy().convert('RGBA')
    frame = frame.resize((640,640))
    #frame.show()
    frame.paste(ImageSequence.Iterator(body)[idx], mask=ImageSequence.Iterator(body)[idx].convert("RGBA"))
    frame.paste(ImageSequence.Iterator(eyes)[idx], mask=ImageSequence.Iterator(eyes)[idx].convert("RGBA"))
    frame.paste(ImageSequence.Iterator(headwear)[idx], mask=ImageSequence.Iterator(headwear)[idx].convert("RGBA"))
    
    frames.append(frame)

print(frames)

frames[0].save('images/generated/output.gif', save_all=True, append_images=frames[1:], loop=0)

@radarhere
Copy link
Member

Please create a new issue and attach the GIFs you are trying to load.

@radarhere
Copy link
Member

The issue from @tomhaydn became #5837

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants