Merge pull request #3005 from radarhere/icns_append

Allow append_images to set .icns scaled images
This commit is contained in:
Hugo 2018-03-19 11:26:30 +02:00 committed by GitHub
commit f5968fdba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 13 deletions

View File

@ -9,7 +9,7 @@ min_iterations = 100
max_iterations = 10000 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): class TestImagingLeaks(PillowTestCase):
def _get_mem_usage(self): def _get_mem_usage(self):

View File

@ -11,7 +11,7 @@ codecs = dir(Image.core)
test_file = "Tests/images/rgb_trns_ycbc.jp2" 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): class TestJpegLeaks(PillowTestCase):
def setUp(self): def setUp(self):
if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs: if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs:

View File

@ -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): class TestJpegLeaks(PillowTestCase):
""" """

View File

@ -227,7 +227,7 @@ class PillowTestCase(unittest.TestCase):
raise IOError() 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): class PillowLeakTestCase(PillowTestCase):
# requires unix/osx # requires unix/osx
iterations = 100 # count iterations = 100 # count

View File

@ -22,8 +22,7 @@ class TestFileIcns(PillowTestCase):
self.assertEqual(im.size, (1024, 1024)) self.assertEqual(im.size, (1024, 1024))
self.assertEqual(im.format, "ICNS") self.assertEqual(im.format, "ICNS")
@unittest.skipIf(sys.platform != 'darwin', @unittest.skipIf(sys.platform != 'darwin', "requires macOS")
"requires MacOS")
def test_save(self): def test_save(self):
im = Image.open(TEST_FILE) im = Image.open(TEST_FILE)
@ -36,6 +35,22 @@ class TestFileIcns(PillowTestCase):
self.assertEqual(reread.size, (1024, 1024)) self.assertEqual(reread.size, (1024, 1024))
self.assertEqual(reread.format, "ICNS") 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): def test_sizes(self):
# Check that we can load all of the sizes, and that the final pixel # Check that we can load all of the sizes, and that the final pixel
# dimensions are as expected # dimensions are as expected

View File

@ -540,7 +540,7 @@ class TestFilePng(PillowTestCase):
self.assertEqual(len(chunks), 3) 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): class TestTruncatedPngPLeaks(PillowLeakTestCase):
mem_limit = 2*1024 # max increase in K mem_limit = 2*1024 # max increase in K
iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs

View File

@ -4,7 +4,7 @@ import sys
from PIL import Image, features, ImageDraw, ImageFont 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): class TestTTypeFontLeak(PillowLeakTestCase):
# fails at iteration 3 in master # fails at iteration 3 in master
iterations = 10 iterations = 10

View File

@ -437,7 +437,7 @@ class TestImageFont(PillowTestCase):
self.assertEqual(('FreeMono', 'Regular'), name) self.assertEqual(('FreeMono', 'Regular'), name)
@unittest.skipIf(sys.platform.startswith('win32'), @unittest.skipIf(sys.platform.startswith('win32'),
"requires Unix or MacOS") "requires Unix or macOS")
def test_find_linux_font(self): def test_find_linux_font(self):
# A lot of mocking here - this is more for hitting code and # A lot of mocking here - this is more for hitting code and
# catching syntax like errors # catching syntax like errors
@ -471,7 +471,7 @@ class TestImageFont(PillowTestCase):
font_directory+'/Duplicate.ttf', 'Duplicate') font_directory+'/Duplicate.ttf', 'Duplicate')
@unittest.skipIf(sys.platform.startswith('win32'), @unittest.skipIf(sys.platform.startswith('win32'),
"requires Unix or MacOS") "requires Unix or macOS")
def test_find_macos_font(self): def test_find_macos_font(self):
# Like the linux test, more cover hitting code rather than testing # Like the linux test, more cover hitting code rather than testing
# correctness. # correctness.

View File

@ -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): class TestShellInjection(PillowTestCase):
def assert_save_filename_check(self, src_img, save_func): def assert_save_filename_check(self, src_img, save_func):

View File

@ -310,6 +310,8 @@ def _save(im, fp, filename):
# create the temporary set of pngs # create the temporary set of pngs
iconset = tempfile.mkdtemp('.iconset') iconset = tempfile.mkdtemp('.iconset')
provided_images = {im.width:im for im in
im.encoderinfo.get("append_images", [])}
last_w = None last_w = None
for w in [16, 32, 128, 256, 512]: for w in [16, 32, 128, 256, 512]:
prefix = 'icon_{}x{}'.format(w, w) prefix = 'icon_{}x{}'.format(w, w)
@ -318,10 +320,12 @@ def _save(im, fp, filename):
if last_w == w: if last_w == w:
shutil.copyfile(second_path, first_path) shutil.copyfile(second_path, first_path)
else: 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') 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 last_w = w*2
# iconutil -c icns -o {} {} # iconutil -c icns -o {} {}