Allow append_images to set .icns scaled images

This commit is contained in:
Andrew Murray 2018-03-19 19:38:07 +11:00
parent 4247de3604
commit 2123dd70fb
2 changed files with 23 additions and 4 deletions

View File

@ -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

View File

@ -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 {} {}