Skip to content

Commit 4cdbeaf

Browse files
authored
Merge pull request #2 from radarhere/comment_correct_placement
Once comment is loaded, keep it for subsequent frames
2 parents db76eaa + 62d0f0e commit 4cdbeaf

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

Tests/images/second_frame_comment.gif

2.34 KB
Loading

Tests/test_file_gif.py

+39-22
Original file line numberDiff line numberDiff line change
@@ -837,31 +837,48 @@ def test_read_multiple_comment_blocks():
837837
assert im.info["comment"] == b"Test comment 1\nTest comment 2"
838838

839839

840-
def test_write_comment(tmp_path):
840+
def test_empty_string_comment(tmp_path):
841841
out = str(tmp_path / "temp.gif")
842-
with Image.open("Tests/images/dispose_prev.gif") as im:
843-
im.save(out, save_all=True, comment="Test")
844-
with Image.open(out) as reread:
845-
# Comments written should appear only in first frame
846-
assert reread.info["comment"] == b"Test"
847-
for i, frame in enumerate(ImageSequence.Iterator(reread)):
848-
assert (
849-
i == 0
850-
and frame.info["comment"] == b"Test"
851-
or i != 0
852-
and "comment" not in frame.info
853-
)
854-
855-
856-
def test_write_no_comment(tmp_path):
842+
with Image.open("Tests/images/chi.gif") as im:
843+
assert "comment" in im.info
844+
845+
# Empty string comment should suppress existing comment
846+
im.save(out, save_all=True, comment="")
847+
848+
with Image.open(out) as reread:
849+
for frame in ImageSequence.Iterator(reread):
850+
assert "comment" not in frame.info
851+
852+
853+
def test_retain_comment_in_subsequent_frames(tmp_path):
854+
# Test that a comment block at the beginning is kept
855+
with Image.open("Tests/images/chi.gif") as im:
856+
for frame in ImageSequence.Iterator(im):
857+
assert frame.info["comment"] == b"Created with GIMP"
858+
859+
with Image.open("Tests/images/second_frame_comment.gif") as im:
860+
assert "comment" not in im.info
861+
862+
# Test that a comment in the middle is read
863+
im.seek(1)
864+
assert im.info["comment"] == b"Comment in the second frame"
865+
866+
# Test that it is still present in a later frame
867+
im.seek(2)
868+
assert im.info["comment"] == b"Comment in the second frame"
869+
870+
# Test that rewinding removes the comment
871+
im.seek(0)
872+
assert "comment" not in im.info
873+
874+
# Test that a saved image keeps the comment
857875
out = str(tmp_path / "temp.gif")
858876
with Image.open("Tests/images/dispose_prev.gif") as im:
859-
# Empty comment="" arg should suppress all comments
860-
im.save(out, save_all=True, comment="")
861-
with Image.open(out) as reread:
862-
assert "comment" not in reread.info
863-
for frame in ImageSequence.Iterator(reread):
864-
assert "comment" not in frame.info
877+
im.save(out, save_all=True, comment="Test")
878+
879+
with Image.open(out) as reread:
880+
for frame in ImageSequence.Iterator(reread):
881+
assert frame.info["comment"] == b"Test"
865882

866883

867884
def test_version(tmp_path):

src/PIL/GifImagePlugin.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def _seek(self, frame, update_image=True):
163163
self.__frame = -1
164164
self._fp.seek(self.__rewind)
165165
self.disposal_method = 0
166+
if "comment" in self.info:
167+
del self.info["comment"]
166168
else:
167169
# ensure that the previous frame was loaded
168170
if self.tile and update_image:
@@ -230,7 +232,7 @@ def _seek(self, frame, update_image=True):
230232
#
231233
comment = b""
232234

233-
# Collect one comment block
235+
# Read this comment block
234236
while block:
235237
comment += block
236238
block = self.data()
@@ -395,7 +397,9 @@ def _rgb(color):
395397
)
396398
]
397399

398-
for k in ["duration", "comment", "extension", "loop"]:
400+
if info.get("comment"):
401+
self.info["comment"] = info["comment"]
402+
for k in ["duration", "extension", "loop"]:
399403
if k in info:
400404
self.info[k] = info[k]
401405
elif k in self.info:
@@ -929,17 +933,18 @@ def _get_global_header(im, info):
929933
# Global Color Table
930934
_get_header_palette(palette_bytes),
931935
]
936+
if info.get("comment"):
937+
comment_block = b"!" + o8(254) # extension intro
932938

933-
if "comment" in info and len(info["comment"]):
934939
comment = info["comment"]
935940
if isinstance(comment, str):
936941
comment = comment.encode()
937-
header.append(b"!" + o8(254)) # extension intro
938942
for i in range(0, len(comment), 255):
939943
subblock = comment[i : i + 255]
940-
header.append(o8(len(subblock)) + subblock)
941-
header.append(o8(0))
944+
comment_block += o8(len(subblock)) + subblock
942945

946+
comment_block += o8(0)
947+
header.append(comment_block)
943948
return header
944949

945950

0 commit comments

Comments
 (0)