From 58d5a73fac7b4084fe4ba66a1a2c4ed097d0d557 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Mon, 17 Jun 2024 09:29:14 -0500 Subject: [PATCH] raise SyntaxError when parsing codestream rather than returning an empty string Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/Jpeg2KImagePlugin.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/PIL/Jpeg2KImagePlugin.py b/src/PIL/Jpeg2KImagePlugin.py index 39eb1c20314..e50cd7799a3 100644 --- a/src/PIL/Jpeg2KImagePlugin.py +++ b/src/PIL/Jpeg2KImagePlugin.py @@ -97,7 +97,7 @@ def next_box_type(self) -> bytes: return tbox -def _parse_codestream(fp): +def _parse_codestream(fp) -> tuple[tuple[int, int], str]: """Parse the JPEG 2000 codestream to extract the size and component count from the SIZ marker segment, returning a PIL (size, mode) tuple.""" @@ -122,7 +122,8 @@ def _parse_codestream(fp): elif csiz == 4: mode = "RGBA" else: - mode = "" + msg = "unable to determine J2K image mode" + raise SyntaxError(msg) return size, mode @@ -237,10 +238,6 @@ def _open(self) -> None: msg = "not a JPEG 2000 file" raise SyntaxError(msg) - if self.size is None or not self.mode: - msg = "unable to determine size/mode" - raise SyntaxError(msg) - self._reduce = 0 self.layers = 0