Skip to content

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

Closed

Description

@raygard

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions