From 2123dd70fbf88c6cd9db9d24ccff57f313e75c14 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 19 Mar 2018 19:38:07 +1100 Subject: [PATCH 1/2] 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 {} {} From 4a781d0013c0c32042dcb3afba70c385b17473c8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 19 Mar 2018 19:36:07 +1100 Subject: [PATCH 2/2] Replaced MacOS with macOS --- Tests/check_imaging_leaks.py | 2 +- Tests/check_j2k_leaks.py | 2 +- Tests/check_jpeg_leaks.py | 2 +- Tests/helper.py | 2 +- Tests/test_file_icns.py | 4 ++-- Tests/test_file_png.py | 2 +- Tests/test_font_leaks.py | 2 +- Tests/test_imagefont.py | 4 ++-- Tests/test_shell_injection.py | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Tests/check_imaging_leaks.py b/Tests/check_imaging_leaks.py index 00a0245e4..eb9426eee 100755 --- a/Tests/check_imaging_leaks.py +++ b/Tests/check_imaging_leaks.py @@ -9,7 +9,7 @@ min_iterations = 100 max_iterations = 10000 -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class TestImagingLeaks(PillowTestCase): def _get_mem_usage(self): diff --git a/Tests/check_j2k_leaks.py b/Tests/check_j2k_leaks.py index 8e9c4ca20..5fafccbc0 100755 --- a/Tests/check_j2k_leaks.py +++ b/Tests/check_j2k_leaks.py @@ -11,7 +11,7 @@ codecs = dir(Image.core) test_file = "Tests/images/rgb_trns_ycbc.jp2" -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class TestJpegLeaks(PillowTestCase): def setUp(self): if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs: diff --git a/Tests/check_jpeg_leaks.py b/Tests/check_jpeg_leaks.py index 7df2dfcc4..065e9d817 100644 --- a/Tests/check_jpeg_leaks.py +++ b/Tests/check_jpeg_leaks.py @@ -15,7 +15,7 @@ NOSE_PROCESSES=0 NOSE_TIMEOUT=600 valgrind --tool=massif \ """ -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class TestJpegLeaks(PillowTestCase): """ diff --git a/Tests/helper.py b/Tests/helper.py index 8556ed76a..7036ff898 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -227,7 +227,7 @@ class PillowTestCase(unittest.TestCase): raise IOError() -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class PillowLeakTestCase(PillowTestCase): # requires unix/osx iterations = 100 # count diff --git a/Tests/test_file_icns.py b/Tests/test_file_icns.py index a347be41a..d8d6a128e 100644 --- a/Tests/test_file_icns.py +++ b/Tests/test_file_icns.py @@ -22,7 +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) @@ -35,7 +35,7 @@ class TestFileIcns(PillowTestCase): self.assertEqual(reread.size, (1024, 1024)) self.assertEqual(reread.format, "ICNS") - @unittest.skipIf(sys.platform != 'darwin', "requires MacOS") + @unittest.skipIf(sys.platform != 'darwin', "requires macOS") def test_save_append_images(self): im = Image.open(TEST_FILE) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 8541c5f24..8cf109e70 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -540,7 +540,7 @@ class TestFilePng(PillowTestCase): self.assertEqual(len(chunks), 3) -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class TestTruncatedPngPLeaks(PillowLeakTestCase): mem_limit = 2*1024 # max increase in K iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs diff --git a/Tests/test_font_leaks.py b/Tests/test_font_leaks.py index a1c40e6a9..4de28a95a 100644 --- a/Tests/test_font_leaks.py +++ b/Tests/test_font_leaks.py @@ -4,7 +4,7 @@ import sys from PIL import Image, features, ImageDraw, ImageFont -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class TestTTypeFontLeak(PillowLeakTestCase): # fails at iteration 3 in master iterations = 10 diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index e120f8482..0ec49384e 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -437,7 +437,7 @@ class TestImageFont(PillowTestCase): self.assertEqual(('FreeMono', 'Regular'), name) @unittest.skipIf(sys.platform.startswith('win32'), - "requires Unix or MacOS") + "requires Unix or macOS") def test_find_linux_font(self): # A lot of mocking here - this is more for hitting code and # catching syntax like errors @@ -471,7 +471,7 @@ class TestImageFont(PillowTestCase): font_directory+'/Duplicate.ttf', 'Duplicate') @unittest.skipIf(sys.platform.startswith('win32'), - "requires Unix or MacOS") + "requires Unix or macOS") def test_find_macos_font(self): # Like the linux test, more cover hitting code rather than testing # correctness. diff --git a/Tests/test_shell_injection.py b/Tests/test_shell_injection.py index acfea3bae..9e489f77c 100644 --- a/Tests/test_shell_injection.py +++ b/Tests/test_shell_injection.py @@ -18,7 +18,7 @@ test_filenames = ( ) -@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS") +@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS") class TestShellInjection(PillowTestCase): def assert_save_filename_check(self, src_img, save_func):