Correct placement of GIF comment

Place GIF comment after Global Color table. Should go after "NETSCAPE" looping extension after pull #6211.
This commit is contained in:
Ray Gardner 2022-05-13 12:45:01 -06:00
parent c4325c805e
commit 22d9095e5c
2 changed files with 36 additions and 10 deletions

View File

@ -813,6 +813,29 @@ def test_zero_comment_subblocks():
assert_image_equal_tofile(im, TEST_GIF) assert_image_equal_tofile(im, TEST_GIF)
def test_write_comment(tmp_path):
out = str(tmp_path / "temp.gif")
with Image.open("Tests/images/multiple_comments.gif") as im:
im.save(out, save_all=True, comment="Test")
with Image.open(out) as reread:
# Comments written should appear only in first frame
assert reread.info["comment"] == b"Test"
for i, frame in enumerate(ImageSequence.Iterator(reread)):
assert (i == 0 and frame.info["comment"] == b"Test" or
i != 0 and "comment" not in frame.info)
def test_write_no_comment(tmp_path):
out = str(tmp_path / "temp.gif")
with Image.open("Tests/images/multiple_comments.gif") as im:
# Empty comment="" arg should suppress all comments
im.save(out, save_all=True, comment="")
with Image.open(out) as reread:
assert "comment" not in reread.info
for frame in ImageSequence.Iterator(reread):
assert "comment" not in frame.info
def test_version(tmp_path): def test_version(tmp_path):
out = str(tmp_path / "temp.gif") out = str(tmp_path / "temp.gif")

View File

@ -715,15 +715,6 @@ def _write_local_header(fp, im, offset, flags):
+ o8(0) + o8(0)
) )
if "comment" in im.encoderinfo and 1 <= len(im.encoderinfo["comment"]):
fp.write(b"!" + o8(254)) # extension intro
comment = im.encoderinfo["comment"]
if isinstance(comment, str):
comment = comment.encode()
for i in range(0, len(comment), 255):
subblock = comment[i : i + 255]
fp.write(o8(len(subblock)) + subblock)
fp.write(o8(0))
if "loop" in im.encoderinfo: if "loop" in im.encoderinfo:
number_of_loops = im.encoderinfo["loop"] number_of_loops = im.encoderinfo["loop"]
fp.write( fp.write(
@ -929,7 +920,7 @@ def _get_global_header(im, info):
palette_bytes = _get_palette_bytes(im) palette_bytes = _get_palette_bytes(im)
color_table_size = _get_color_table_size(palette_bytes) color_table_size = _get_color_table_size(palette_bytes)
return [ header = [
b"GIF" # signature b"GIF" # signature
+ version # version + version # version
+ o16(im.size[0]) # canvas width + o16(im.size[0]) # canvas width
@ -943,6 +934,18 @@ def _get_global_header(im, info):
_get_header_palette(palette_bytes), _get_header_palette(palette_bytes),
] ]
if "comment" in info and len(info["comment"]):
comment = info["comment"]
if isinstance(comment, str):
comment = comment.encode()
header.append(b"!" + o8(254)) # extension intro
for i in range(0, len(comment), 255):
subblock = comment[i : i + 255]
header.append(o8(len(subblock)) + subblock)
header.append(o8(0))
return header
def _write_frame_data(fp, im_frame, offset, params): def _write_frame_data(fp, im_frame, offset, params):
try: try: