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

Improperly saving gif frame with some gif images #5734

Closed
mrnextdoor0000 opened this issue Sep 26, 2021 · 7 comments · Fixed by #5857
Closed

Improperly saving gif frame with some gif images #5734

mrnextdoor0000 opened this issue Sep 26, 2021 · 7 comments · Fixed by #5857
Labels

Comments

@mrnextdoor0000
Copy link

mrnextdoor0000 commented Sep 26, 2021

from PIL import Image, ImageSequence

def getframe(src, out):
    with Image.open(src) as img:
        print(f'file opened')
        i = 0
        for frame in ImageSequence.Iterator(img):
            if i == 1:
                frame.save(out)
                break
            else:
                i += 1

i used this method to get a 2nd frame of a gif image.
i got the frame but it was not good.
source gif:
test7

result 2nd frame i got (gif format):
result7

i tried to get 1st frame and 1st frame was very good unlike the 2nd.

i also tried another gif image has transparency.
i added convert function before save the frame

frame = frame.convert('RGB')

source gif:
test8

result 2nd frame i got (jpg format):
result8

i think the second case was occured since i did not set the background color. But there is no options about background of jpeg with Image.save().

There is some bug.. or something i missed.
Please let me know, thank you.

i used python 3.9.7 and pillow 8.3.2

@radarhere radarhere added the GIF label Sep 26, 2021
@radarhere radarhere changed the title improperly saving gif frame with some gif images Improperly saving gif frame with some gif images Sep 26, 2021
@mrnextdoor0000
Copy link
Author

mrnextdoor0000 commented Sep 27, 2021

I tried to get frames of Merry Christmas file with another tools, photoscape, photoshop and they gave fine frames. So I think it might be a problem with decoding gif format in pillow.
I read #1525, tried to paste a converted frame to a previous frame. It did not work. The converted frame was pasted with its noise.

@radarhere
Copy link
Member

There is a known bug in Pillow when reading subsequent GIF frames with different palettes. For example, see #4977 for instance, which bizarrely enough, is dealing with your exact same image.

@mrnextdoor0000
Copy link
Author

I found similar issues, #1692 , #1717 . It seems that no one has solved it in about 5 years..

@wiredfool
Copy link
Member

@mrnextdoor0000 Feel free to send a PR!

@tomhaydn
Copy link

Related to #5837 ?

@radarhere
Copy link
Member

radarhere commented Nov 29, 2021

#5857 will fix the first image. Here is the second frame with that PR.

out

@radarhere
Copy link
Member

For the second image, the background color is actually coming from the image. The way that GIF stores transparency is by designating a certain color to the "transparent" one. So when you remove that transparency in order to save it as a non-transparent JPEG, it becomes visible.

If you want to select a background color, here is an example where green is the background color -

from PIL import Image

with Image.open("source.gif") as img:
    img.seek(1)
    img = img.convert("RGBA")
    background = Image.new("RGB", img.size, "#0f0")
    background.paste(img, (0, 0), img)
    background.save("out.jpg")

out

Let us know if that isn't satisfactory for any reason.

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

Successfully merging a pull request may close this issue.

4 participants