From 2123dd70fbf88c6cd9db9d24ccff57f313e75c14 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 19 Mar 2018 19:38:07 +1100 Subject: [PATCH] Allow append_images to set .icns scaled images --- Tests/test_file_icns.py | 19 +++++++++++++++++-- src/PIL/IcnsImagePlugin.py | 8 ++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index d8508e579..a347be41a 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -22,8 +22,7 @@ class TestFileIcns(PillowTestCase): self.assertEqual(im.size, (1024, 1024)) self.assertEqual(im.format, "ICNS") - @unittest.skipIf(sys.platform != 'darwin', - "requires MacOS") + @unittest.skipIf(sys.platform != 'darwin', "requires MacOS") def test_save(self): im = Image.open(TEST_FILE) @@ -36,6 +35,22 @@ class TestFileIcns(PillowTestCase): self.assertEqual(reread.size, (1024, 1024)) self.assertEqual(reread.format, "ICNS") + @unittest.skipIf(sys.platform != 'darwin', "requires MacOS") + def test_save_append_images(self): + im = Image.open(TEST_FILE) + + temp_file = self.tempfile("temp.icns") + provided_im = Image.new('RGBA', (32, 32), (255, 0, 0, 0)) + im.save(temp_file, append_images=[provided_im]) + + reread = Image.open(temp_file) + self.assert_image_equal(reread, im) + + reread = Image.open(temp_file) + reread.size = (16, 16, 2) + reread.load() + self.assert_image_equal(reread, provided_im) + def test_sizes(self): # Check that we can load all of the sizes, and that the final pixel # dimensions are as expected diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 1dce3fe4b..b382a73e1 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -310,6 +310,8 @@ def _save(im, fp, filename): # create the temporary set of pngs iconset = tempfile.mkdtemp('.iconset') + provided_images = {im.width:im for im in + im.encoderinfo.get("append_images", [])} last_w = None for w in [16, 32, 128, 256, 512]: prefix = 'icon_{}x{}'.format(w, w) @@ -318,10 +320,12 @@ def _save(im, fp, filename): if last_w == w: shutil.copyfile(second_path, first_path) else: - im.resize((w, w), Image.LANCZOS).save(first_path) + im_w = provided_images.get(w, im.resize((w, w), Image.LANCZOS)) + im_w.save(first_path) second_path = os.path.join(iconset, prefix+'@2x.png') - im.resize((w*2, w*2), Image.LANCZOS).save(second_path) + im_w2 = provided_images.get(w*2, im.resize((w*2, w*2), Image.LANCZOS)) + im_w2.save(second_path) last_w = w*2 # iconutil -c icns -o {} {}