Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not read layers immediately when opening PSD images #8039

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Tests/test_file_psd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from PIL import Image, PsdImagePlugin, UnidentifiedImageError
from PIL import Image, PsdImagePlugin

from .helper import assert_image_equal_tofile, assert_image_similar, hopper, is_pypy

Expand Down Expand Up @@ -150,14 +150,6 @@ def test_combined_larger_than_size() -> None:
@pytest.mark.parametrize(
"test_file,raises",
[
(
"Tests/images/timeout-1ee28a249896e05b83840ae8140622de8e648ba9.psd",
UnidentifiedImageError,
),
(
"Tests/images/timeout-598843abc37fc080ec36a2699ebbd44f795d3a6f.psd",
UnidentifiedImageError,
),
("Tests/images/timeout-c8efc3fded6426986ba867a399791bae544f59bc.psd", OSError),
("Tests/images/timeout-dedc7a4ebd856d79b4359bbcc79e8ef231ce38f6.psd", OSError),
],
Expand All @@ -167,3 +159,17 @@ def test_crashes(test_file: str, raises) -> None:
with pytest.raises(raises):
with Image.open(f):
pass


@pytest.mark.parametrize(
"test_file",
[
"Tests/images/timeout-1ee28a249896e05b83840ae8140622de8e648ba9.psd",
"Tests/images/timeout-598843abc37fc080ec36a2699ebbd44f795d3a6f.psd",
],
)
def test_layer_crashes(test_file: str) -> None:
with open(test_file, "rb") as f:
with Image.open(f) as im:
with pytest.raises(SyntaxError):
im.layers
30 changes: 25 additions & 5 deletions src/PIL/PsdImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from __future__ import annotations

import io
from functools import cached_property

from . import Image, ImageFile, ImagePalette
from ._binary import i8
Expand Down Expand Up @@ -118,18 +119,17 @@ def _open(self) -> None:
#
# layer and mask information

self.layers = []
self._layers_position = None

size = i32(read(4))
if size:
end = self.fp.tell() + size
size = i32(read(4))
if size:
_layer_data = io.BytesIO(ImageFile._safe_read(self.fp, size))
self.layers = _layerinfo(_layer_data, size)
self._layers_position = self.fp.tell()
self._layers_size = size
self.fp.seek(end)
self.n_frames = len(self.layers)
self.is_animated = self.n_frames > 1
self._n_frames: int | None = None

#
# image descriptor
Expand All @@ -141,6 +141,26 @@ def _open(self) -> None:
self.frame = 1
self._min_frame = 1

@cached_property
def layers(self):
layers = []
if self._layers_position is not None:
self._fp.seek(self._layers_position)
_layer_data = io.BytesIO(ImageFile._safe_read(self._fp, self._layers_size))
layers = _layerinfo(_layer_data, self._layers_size)
self._n_frames = len(layers)
return layers

@property
def n_frames(self) -> int:
if self._n_frames is None:
self._n_frames = len(self.layers)
return self._n_frames

@property
def is_animated(self) -> bool:
return len(self.layers) > 1

def seek(self, layer: int) -> None:
if not self._seek_check(layer):
return
Expand Down
Loading