Improved consistency of returning an image access object from load()

This commit is contained in:
Andrew Murray 2022-02-02 11:49:31 +11:00
parent 369124f3c8
commit fb7edfda68
13 changed files with 76 additions and 39 deletions

View File

@ -58,6 +58,15 @@ def test_sanity():
assert image2_scale2.format == "EPS" assert image2_scale2.format == "EPS"
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_load():
with Image.open(FILE1) as im:
assert im.load()[0, 0] == (255, 255, 255)
# Test again now that it has already been loaded once
assert im.load()[0, 0] == (255, 255, 255)
def test_invalid_file(): def test_invalid_file():
invalid_file = "Tests/images/flower.jpg" invalid_file = "Tests/images/flower.jpg"

View File

@ -5,20 +5,28 @@ from PIL import GbrImagePlugin, Image
from .helper import assert_image_equal_tofile from .helper import assert_image_equal_tofile
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
GbrImagePlugin.GbrImageFile(invalid_file)
def test_gbr_file(): def test_gbr_file():
with Image.open("Tests/images/gbr.gbr") as im: with Image.open("Tests/images/gbr.gbr") as im:
assert_image_equal_tofile(im, "Tests/images/gbr.png") assert_image_equal_tofile(im, "Tests/images/gbr.png")
def test_load():
with Image.open("Tests/images/gbr.gbr") as im:
assert im.load()[0, 0] == (0, 0, 0, 0)
# Test again now that it has already been loaded once
assert im.load()[0, 0] == (0, 0, 0, 0)
def test_multiple_load_operations(): def test_multiple_load_operations():
with Image.open("Tests/images/gbr.gbr") as im: with Image.open("Tests/images/gbr.gbr") as im:
im.load() im.load()
im.load() im.load()
assert_image_equal_tofile(im, "Tests/images/gbr.png") assert_image_equal_tofile(im, "Tests/images/gbr.png")
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
GbrImagePlugin.GbrImageFile(invalid_file)

View File

@ -28,6 +28,14 @@ def test_sanity():
assert im.format == "ICNS" assert im.format == "ICNS"
def test_load():
with Image.open(TEST_FILE) as im:
assert im.load()[0, 0] == (0, 0, 0, 0)
# Test again now that it has already been loaded once
assert im.load()[0, 0] == (0, 0, 0, 0)
def test_save(tmp_path): def test_save(tmp_path):
temp_file = str(tmp_path / "temp.icns") temp_file = str(tmp_path / "temp.icns")

View File

@ -18,6 +18,11 @@ def test_sanity():
assert im.get_format_mimetype() == "image/x-icon" assert im.get_format_mimetype() == "image/x-icon"
def test_load():
with Image.open(TEST_ICO_FILE) as im:
assert im.load()[0, 0] == (1, 1, 9, 255)
def test_mask(): def test_mask():
with Image.open("Tests/images/hopper_mask.ico") as im: with Image.open("Tests/images/hopper_mask.ico") as im:
assert_image_equal_tofile(im, "Tests/images/hopper_mask.png") assert_image_equal_tofile(im, "Tests/images/hopper_mask.png")

View File

@ -2,15 +2,11 @@ from PIL import WalImageFile
from .helper import assert_image_equal_tofile from .helper import assert_image_equal_tofile
TEST_FILE = "Tests/images/hopper.wal"
def test_open(): def test_open():
# Arrange
TEST_FILE = "Tests/images/hopper.wal"
# Act
with WalImageFile.open(TEST_FILE) as im: with WalImageFile.open(TEST_FILE) as im:
# Assert
assert im.format == "WAL" assert im.format == "WAL"
assert im.format_description == "Quake2 Texture" assert im.format_description == "Quake2 Texture"
assert im.mode == "P" assert im.mode == "P"
@ -19,3 +15,11 @@ def test_open():
assert isinstance(im, WalImageFile.WalImageFile) assert isinstance(im, WalImageFile.WalImageFile)
assert_image_equal_tofile(im, "Tests/images/hopper_wal.png") assert_image_equal_tofile(im, "Tests/images/hopper_wal.png")
def test_load():
with WalImageFile.open(TEST_FILE) as im:
assert im.load()[0, 0] == 122
# Test again now that it has already been loaded once
assert im.load()[0, 0] == 122

View File

@ -24,6 +24,12 @@ def test_load_raw():
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref.png", 2.0) assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref.png", 2.0)
def test_load():
with Image.open("Tests/images/drawing.emf") as im:
if hasattr(Image.core, "drawwmf"):
assert im.load()[0, 0] == (255, 255, 255)
def test_register_handler(tmp_path): def test_register_handler(tmp_path):
class TestHandler: class TestHandler:
methodCalled = False methodCalled = False

View File

@ -329,12 +329,12 @@ class EpsImageFile(ImageFile.ImageFile):
def load(self, scale=1, transparency=False): def load(self, scale=1, transparency=False):
# Load EPS via Ghostscript # Load EPS via Ghostscript
if not self.tile: if self.tile:
return
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency) self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)
self.mode = self.im.mode self.mode = self.im.mode
self._size = self.im.size self._size = self.im.size
self.tile = [] self.tile = []
return Image.Image.load(self)
def load_seek(self, *args, **kwargs): def load_seek(self, *args, **kwargs):
# we can't incrementally load, so force ImageFile.parser to # we can't incrementally load, so force ImageFile.parser to

View File

@ -84,12 +84,10 @@ class GbrImageFile(ImageFile.ImageFile):
self._data_size = width * height * color_depth self._data_size = width * height * color_depth
def load(self): def load(self):
if self.im: if not self.im:
# Already loaded
return
self.im = Image.core.new(self.mode, self.size) self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self._data_size)) self.frombytes(self.fp.read(self._data_size))
return Image.Image.load(self)
# #

View File

@ -286,21 +286,22 @@ class IcnsImageFile(ImageFile.ImageFile):
self.best_size[1] * self.best_size[2], self.best_size[1] * self.best_size[2],
) )
Image.Image.load(self) px = Image.Image.load(self)
if self.im and self.im.size == self.size: if self.im and self.im.size == self.size:
# Already loaded # Already loaded
return return px
self.load_prepare() self.load_prepare()
# This is likely NOT the best way to do it, but whatever. # This is likely NOT the best way to do it, but whatever.
im = self.icns.getimage(self.best_size) im = self.icns.getimage(self.best_size)
# If this is a PNG or JPEG 2000, it won't be loaded yet # If this is a PNG or JPEG 2000, it won't be loaded yet
im.load() px = im.load()
self.im = im.im self.im = im.im
self.mode = im.mode self.mode = im.mode
self.size = im.size self.size = im.size
self.load_end()
return px
def _save(im, fp, filename): def _save(im, fp, filename):

View File

@ -306,7 +306,7 @@ class IcoImageFile(ImageFile.ImageFile):
def load(self): def load(self):
if self.im and self.im.size == self.size: if self.im and self.im.size == self.size:
# Already loaded # Already loaded
return return Image.Image.load(self)
im = self.ico.getimage(self.size) im = self.ico.getimage(self.size)
# if tile is PNG, it won't really be loaded yet # if tile is PNG, it won't really be loaded yet
im.load() im.load()

View File

@ -328,6 +328,7 @@ class StubImageFile(ImageFile):
# become the other object (!) # become the other object (!)
self.__class__ = image.__class__ self.__class__ = image.__class__
self.__dict__ = image.__dict__ self.__dict__ = image.__dict__
return image.load()
def _load(self): def _load(self):
"""(Hook) Find actual image loader.""" """(Hook) Find actual image loader."""

View File

@ -51,14 +51,11 @@ class WalImageFile(ImageFile.ImageFile):
self.info["next_name"] = next_name self.info["next_name"] = next_name
def load(self): def load(self):
if self.im: if not self.im:
# Already loaded
return
self.im = Image.core.new(self.mode, self.size) self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self.size[0] * self.size[1])) self.frombytes(self.fp.read(self.size[0] * self.size[1]))
self.putpalette(quake2palette) self.putpalette(quake2palette)
Image.Image.load(self) return Image.Image.load(self)
def open(filename): def open(filename):

View File

@ -158,7 +158,7 @@ class WmfStubImageFile(ImageFile.StubImageFile):
(x1 - x0) * self.info["dpi"] // self._inch, (x1 - x0) * self.info["dpi"] // self._inch,
(y1 - y0) * self.info["dpi"] // self._inch, (y1 - y0) * self.info["dpi"] // self._inch,
) )
super().load() return super().load()
def _save(im, fp, filename): def _save(im, fp, filename):