From a129937cf507c3adc16ab6db9723b99bd872f430 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 11 Oct 2023 23:37:17 +1100 Subject: [PATCH] If Image is created directly, add missing core image when loading --- Tests/test_image.py | 12 ++++++++++++ src/PIL/Image.py | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/Tests/test_image.py b/Tests/test_image.py index 83dac70802f..d1a385a350e 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -94,6 +94,18 @@ def test_sanity(self): # with pytest.raises(MemoryError): # Image.new("L", (1000000, 1000000)) + def test_direct(self): + # Test that a directly instantiated Image() + # is given a core image during load() + im = Image.Image() + assert im.im is None + + im.load() + assert im.im is not None + + # Test equality + assert Image.Image() == Image.Image() + def test_repr_pretty(self): class Pretty: def text(self, text): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a79666d7afe..89034da3a36 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -826,6 +826,10 @@ def load(self): :returns: An image access object. :rtype: :ref:`PixelAccess` or :py:class:`PIL.PyAccess` """ + if self._mode == "": + # This image was instantiated directly + self._mode = "1" + self.im = core.new(self.mode, self.size) if self.im is not None and self.palette and self.palette.dirty: # realize palette mode, arr = self.palette.getdata()