If Image is created directly, add missing core image when loading

This commit is contained in:
Andrew Murray 2023-10-11 23:37:17 +11:00
parent 4ecf1df4ea
commit a129937cf5
2 changed files with 16 additions and 0 deletions

View File

@ -94,6 +94,18 @@ class TestImage:
# 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):

View File

@ -826,6 +826,10 @@ class Image:
: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()