Skip to content

Commit

Permalink
Merge pull request python-pillow#6007 from radarhere/load
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Feb 2, 2022
2 parents 8e3878d + fb7edfd commit eccd853
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 39 deletions.
9 changes: 9 additions & 0 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def test_sanity():
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():
invalid_file = "Tests/images/flower.jpg"

Expand Down
22 changes: 15 additions & 7 deletions Tests/test_file_gbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
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():
with Image.open("Tests/images/gbr.gbr") as im:
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():
with Image.open("Tests/images/gbr.gbr") as im:
im.load()
im.load()
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)
8 changes: 8 additions & 0 deletions Tests/test_file_icns.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def test_sanity():
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):
temp_file = str(tmp_path / "temp.icns")

Expand Down
5 changes: 5 additions & 0 deletions Tests/test_file_ico.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def test_sanity():
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():
with Image.open("Tests/images/hopper_mask.ico") as im:
assert_image_equal_tofile(im, "Tests/images/hopper_mask.png")
Expand Down
16 changes: 10 additions & 6 deletions Tests/test_file_wal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

from .helper import assert_image_equal_tofile

TEST_FILE = "Tests/images/hopper.wal"

def test_open():
# Arrange
TEST_FILE = "Tests/images/hopper.wal"

# Act
def test_open():
with WalImageFile.open(TEST_FILE) as im:

# Assert
assert im.format == "WAL"
assert im.format_description == "Quake2 Texture"
assert im.mode == "P"
Expand All @@ -19,3 +15,11 @@ def test_open():
assert isinstance(im, WalImageFile.WalImageFile)

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
6 changes: 6 additions & 0 deletions Tests/test_file_wmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_load_raw():
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):
class TestHandler:
methodCalled = False
Expand Down
12 changes: 6 additions & 6 deletions src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ def _find_offset(self, fp):

def load(self, scale=1, transparency=False):
# Load EPS via Ghostscript
if not self.tile:
return
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)
self.mode = self.im.mode
self._size = self.im.size
self.tile = []
if self.tile:
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)
self.mode = self.im.mode
self._size = self.im.size
self.tile = []
return Image.Image.load(self)

def load_seek(self, *args, **kwargs):
# we can't incrementally load, so force ImageFile.parser to
Expand Down
10 changes: 4 additions & 6 deletions src/PIL/GbrImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ def _open(self):
self._data_size = width * height * color_depth

def load(self):
if self.im:
# Already loaded
return

self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self._data_size))
if not self.im:
self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self._data_size))
return Image.Image.load(self)


#
Expand Down
9 changes: 5 additions & 4 deletions src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,22 @@ def load(self):
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:
# Already loaded
return
return px
self.load_prepare()
# This is likely NOT the best way to do it, but whatever.
im = self.icns.getimage(self.best_size)

# If this is a PNG or JPEG 2000, it won't be loaded yet
im.load()
px = im.load()

self.im = im.im
self.mode = im.mode
self.size = im.size
self.load_end()

return px


def _save(im, fp, filename):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def size(self, value):
def load(self):
if self.im and self.im.size == self.size:
# Already loaded
return
return Image.Image.load(self)
im = self.ico.getimage(self.size)
# if tile is PNG, it won't really be loaded yet
im.load()
Expand Down
1 change: 1 addition & 0 deletions src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def load(self):
# become the other object (!)
self.__class__ = image.__class__
self.__dict__ = image.__dict__
return image.load()

def _load(self):
"""(Hook) Find actual image loader."""
Expand Down
13 changes: 5 additions & 8 deletions src/PIL/WalImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ def _open(self):
self.info["next_name"] = next_name

def load(self):
if self.im:
# Already loaded
return

self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self.size[0] * self.size[1]))
self.putpalette(quake2palette)
Image.Image.load(self)
if not self.im:
self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self.size[0] * self.size[1]))
self.putpalette(quake2palette)
return Image.Image.load(self)


def open(filename):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/WmfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def load(self, dpi=None):
(x1 - x0) * self.info["dpi"] // self._inch,
(y1 - y0) * self.info["dpi"] // self._inch,
)
super().load()
return super().load()


def _save(im, fp, filename):
Expand Down

0 comments on commit eccd853

Please sign in to comment.