@@ -837,31 +837,48 @@ def test_read_multiple_comment_blocks():
837837 assert im .info ["comment" ] == b"Test comment 1\n Test 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
867884def test_version (tmp_path ):
0 commit comments