Merge pull request #3897 from radarhere/load

Improved ICO and ICNS loading
This commit is contained in:
Hugo 2019-06-19 09:09:34 +03:00 committed by GitHub
commit ac6185ca15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 14 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 B

View File

@ -61,11 +61,10 @@ class TestFileIcns(PillowTestCase):
for w, h, r in im.info['sizes']: for w, h, r in im.info['sizes']:
wr = w * r wr = w * r
hr = h * r hr = h * r
im2 = Image.open(TEST_FILE) im.size = (w, h, r)
im2.size = (w, h, r) im.load()
im2.load() self.assertEqual(im.mode, 'RGBA')
self.assertEqual(im2.mode, 'RGBA') self.assertEqual(im.size, (wr, hr))
self.assertEqual(im2.size, (wr, hr))
# Check that we cannot load an incorrect size # Check that we cannot load an incorrect size
with self.assertRaises(ValueError): with self.assertRaises(ValueError):

View File

@ -1,7 +1,7 @@
from .helper import PillowTestCase, hopper from .helper import PillowTestCase, hopper
import io import io
from PIL import Image, IcoImagePlugin from PIL import Image, ImageDraw, IcoImagePlugin
TEST_ICO_FILE = "Tests/images/hopper.ico" TEST_ICO_FILE = "Tests/images/hopper.ico"
@ -90,3 +90,16 @@ class TestFileIco(PillowTestCase):
im = self.assert_warning(UserWarning, im = self.assert_warning(UserWarning,
Image.open, "Tests/images/hopper_unexpected.ico") Image.open, "Tests/images/hopper_unexpected.ico")
self.assertEqual(im.size, (16, 16)) self.assertEqual(im.size, (16, 16))
def test_draw_reloaded(self):
im = Image.open(TEST_ICO_FILE)
outfile = self.tempfile("temp_saved_hopper_draw.ico")
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, '#f00')
im.save(outfile)
im = Image.open(outfile)
im.save("Tests/images/hopper_draw.ico")
reloaded = Image.open("Tests/images/hopper_draw.ico")
self.assert_image_equal(im, reloaded)

View File

@ -251,8 +251,6 @@ class IcnsImageFile(ImageFile.ImageFile):
self.best_size[0] * self.best_size[2], self.best_size[0] * self.best_size[2],
self.best_size[1] * self.best_size[2], self.best_size[1] * self.best_size[2],
) )
# Just use this to see if it's loaded or not yet.
self.tile = ("",)
@property @property
def size(self): def size(self):
@ -286,7 +284,8 @@ class IcnsImageFile(ImageFile.ImageFile):
) )
Image.Image.load(self) Image.Image.load(self)
if not self.tile: if self.im and self.im.size == self.size:
# Already loaded
return return
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.
@ -298,11 +297,6 @@ class IcnsImageFile(ImageFile.ImageFile):
self.im = im.im self.im = im.im
self.mode = im.mode self.mode = im.mode
self.size = im.size self.size = im.size
if self._exclusive_fp:
self.fp.close()
self.fp = None
self.icns = None
self.tile = ()
self.load_end() self.load_end()

View File

@ -288,6 +288,9 @@ class IcoImageFile(ImageFile.ImageFile):
self._size = value self._size = value
def load(self): def load(self):
if self.im and self.im.size == self.size:
# Already loaded
return
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()