From 3058709c220b33342d06114620ac1fe401a960b2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 1 Jun 2026 10:45:52 +1000 Subject: [PATCH 1/2] Renamed variable to not shadow builtin type --- src/PIL/PsdImagePlugin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py index dd3d5ab95fd..fe5ae20c48e 100644 --- a/src/PIL/PsdImagePlugin.py +++ b/src/PIL/PsdImagePlugin.py @@ -222,12 +222,12 @@ def read(size: int) -> bytes: continue for _ in range(ct_types): - type = i16(read(2)) + channel_id = i16(read(2)) - if type == 65535: + if channel_id == 65535: b = "A" else: - b = "RGBA"[type] + b = "RGBA"[channel_id] bands.append(b) read(4) # size From 067fa8ff37630cadbdd971c48877e95ade981f2b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 1 Jun 2026 11:33:34 +1000 Subject: [PATCH 2/2] Do not raise error from unknown channel id when parsing layers --- Tests/test_file_psd.py | 16 ++++++++++++++++ src/PIL/PsdImagePlugin.py | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 538b1406b36..79cc8bc00a0 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -1,5 +1,6 @@ from __future__ import annotations +import io import sys import warnings @@ -157,6 +158,21 @@ def test_no_icc_profile() -> None: assert "icc_profile" not in im.info +def test_unknown_channel_id() -> None: + with open("Tests/images/rgba.psd", "rb") as fp: + data = fp.read() + + # Set channel id to 4 + data = data[:90] + b"\x00\x04" + data[92:] + + b = io.BytesIO(data) + with Image.open(b) as im: + assert isinstance(im, PsdImagePlugin.PsdImageFile) + + # unknown mode + assert im.layers[0][1] == "" + + def test_combined_larger_than_size() -> None: # The combined size of the individual parts is larger than the # declared 'size' of the extra data field, resulting in a backwards seek. diff --git a/src/PIL/PsdImagePlugin.py b/src/PIL/PsdImagePlugin.py index fe5ae20c48e..201909abde6 100644 --- a/src/PIL/PsdImagePlugin.py +++ b/src/PIL/PsdImagePlugin.py @@ -226,8 +226,10 @@ def read(size: int) -> bytes: if channel_id == 65535: b = "A" - else: + elif channel_id < 4: b = "RGBA"[channel_id] + else: + b = "" bands.append(b) read(4) # size