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

GIF comment blocks should be placed near front of file, once #6299

Closed
raygard opened this issue May 14, 2022 · 0 comments · Fixed by #6300
Closed

GIF comment blocks should be placed near front of file, once #6299

raygard opened this issue May 14, 2022 · 0 comments · Fixed by #6300
Labels

Comments

@raygard
Copy link
Contributor

raygard commented May 14, 2022

What did you do?

Opened an animated GIF, wrote it out with a comment= argument to .save.

What did you expect to happen?

I expected the comment to be written once, preferably at the beginning of the file, per advice in (GIF89a spec)[https://www.w3.org/Graphics/GIF/spec-gif89a.txt]

it is recommended that Comment Extensions do not interfere with Control or Data blocks; they should be located at the beginning or at the end of the Data Stream to the extent possible.

What actually happened?

The comment was repeated in every frame.

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.10.4
  • Pillow: 9.1.0

dispose_prev

from PIL import Image, ImageSequence
with Image.open("dispose_prev.gif") as im:
    frames = ImageSequence.all_frames(im)
    frames[0].save("testout.gif", save_all=True, append_images=frames[1:], comment="Test comment")
    with Image.open("testout.gif") as img:
        for k, frame in enumerate(ImageSequence.Iterator(img)):
            if "comment" in frame.info:
                print(f'{k=} {frame.info["comment"]=}')

Output with 9.1.0:

k=0 frame.info["comment"]=b'Test comment'
k=1 frame.info["comment"]=b'Test comment'
k=2 frame.info["comment"]=b'Test comment'
k=3 frame.info["comment"]=b'Test comment'
k=4 frame.info["comment"]=b'Test comment'

Discussion:

Pillow's handling of GIF comments needs to be changed. A comment can be specified with comment="..." arg to .save() or by putting a comment in the .info dict of the Image object being saved. It is now being written in every frame, and placed after the Graphic Control Extension block (if one is written) and before the Image Descriptor. The placement is not prohibited by the standard, but is contrary to the "Recommendations" in part 24e of the standard and also to the GIF Grammar in Appendix B.

I have written PR #6300 to resolve this. It will write the comment once only, before the first Graphic Control Block (if any) and the first Image Descriptor. After PR #6211 is merged, this should cause the comment to appear just after the "NETSCAPE2.0" looping control block, if one is written.

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.

2 participants