diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 8f2ca58a662..da572ae6338 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -85,6 +85,11 @@ def test_eoferror() -> None: # Test that seeking to the last frame does not raise an error im.seek(n_frames - 1) + # Test seeking past the last frame without calling n_frames first + with Image.open(test_file) as im: + with pytest.raises(EOFError): + im.seek(3) + def test_seek_tell() -> None: with Image.open(test_file) as im: diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py index 69a8703dd8b..dd3d5ab95fd 100644 --- a/src/PIL/PsdImagePlugin.py +++ b/src/PIL/PsdImagePlugin.py @@ -175,6 +175,9 @@ def seek(self, layer: int) -> None: raise self._fp.ex # seek to given layer (1..max) + if layer > len(self.layers): + msg = "no more images in PSD file" + raise EOFError(msg) _, mode, _, tile = self.layers[layer - 1] self._mode = mode self.tile = tile