From 5071692039805253ab37dad9fac2f375935a38af Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 25 Oct 2023 08:52:06 +1100 Subject: [PATCH 1/2] Fixed Image.frombytes() for images with a zero dimension --- Tests/test_image.py | 4 ++++ src/PIL/Image.py | 17 +++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 83dac7080..f82b3a947 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -906,6 +906,10 @@ class TestImage: im = Image.new("RGB", size) assert im.tobytes() == b"" + @pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0))) + def test_zero_frombytes(self, size): + Image.frombytes("RGB", size, b"") + def test_has_transparency_data(self): for mode in ("1", "L", "P", "RGB"): im = Image.new(mode, (1, 1)) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 771cb33c3..0c93f4dc7 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2967,15 +2967,16 @@ def frombytes(mode, size, data, decoder_name="raw", *args): _check_size(size) - # may pass tuple instead of argument list - if len(args) == 1 and isinstance(args[0], tuple): - args = args[0] - - if decoder_name == "raw" and args == (): - args = mode - im = new(mode, size) - im.frombytes(data, decoder_name, args) + if im.width != 0 and im.height != 0: + # may pass tuple instead of argument list + if len(args) == 1 and isinstance(args[0], tuple): + args = args[0] + + if decoder_name == "raw" and args == (): + args = mode + + im.frombytes(data, decoder_name, args) return im From 91f115bead706e7b9b57a9135be82726a843cdda Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 25 Oct 2023 08:52:26 +1100 Subject: [PATCH 2/2] Fixed im.frombytes() for images with a zero dimension --- Tests/test_image.py | 3 +++ src/PIL/Image.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Tests/test_image.py b/Tests/test_image.py index f82b3a947..039eb33d1 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -910,6 +910,9 @@ class TestImage: def test_zero_frombytes(self, size): Image.frombytes("RGB", size, b"") + im = Image.new("RGB", size) + im.frombytes(b"") + def test_has_transparency_data(self): for mode in ("1", "L", "P", "RGB"): im = Image.new(mode, (1, 1)) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0c93f4dc7..cb092f1ae 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -791,6 +791,9 @@ class Image: but loads data into this image instead of creating a new image object. """ + if self.width == 0 or self.height == 0: + return + # may pass tuple instead of argument list if len(args) == 1 and isinstance(args[0], tuple): args = args[0]