diff --git a/Tests/images/duplicate_number_of_loops.gif b/Tests/images/duplicate_number_of_loops.gif new file mode 100644 index 00000000000..ac315ee99f3 Binary files /dev/null and b/Tests/images/duplicate_number_of_loops.gif differ diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index dffd1006f2f..be28364d295 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -742,9 +742,16 @@ def test_number_of_loops(tmp_path): im = Image.new("L", (100, 100), "#000") im.save(out, loop=number_of_loops) with Image.open(out) as reread: - assert reread.info["loop"] == number_of_loops + # Check that even if a subsequent GIF frame has the number of loops specified, + # only the value from the first frame is used + with Image.open("Tests/images/duplicate_number_of_loops.gif") as im: + assert im.info["loop"] == 2 + + im.seek(1) + assert im.info["loop"] == 2 + def test_background(tmp_path): out = str(tmp_path / "temp.gif") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 2c885375523..d5c0f74e24c 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -236,7 +236,7 @@ def _seek(self, frame, update_image=True): block = self.data() s = None continue - elif s[0] == 255: + elif s[0] == 255 and frame == 0: # # application extension # @@ -244,7 +244,7 @@ def _seek(self, frame, update_image=True): if block[:11] == b"NETSCAPE2.0": block = self.data() if len(block) >= 3 and block[0] == 1: - info["loop"] = i16(block, 1) + self.info["loop"] = i16(block, 1) while self.data(): pass @@ -389,7 +389,7 @@ def _rgb(color): ) ] - for k in ["duration", "comment", "extension", "loop"]: + for k in ["duration", "comment", "extension"]: if k in info: self.info[k] = info[k] elif k in self.info: