From 07b24c6e6e266999dbaaa12ffb201f402190cad1 Mon Sep 17 00:00:00 2001 From: ziplantil Date: Sun, 19 Apr 2020 16:29:36 +0300 Subject: [PATCH 1/8] add append_images support for ico, much like icns --- src/PIL/IcoImagePlugin.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index e4a74321b..0186751be 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -52,6 +52,9 @@ def _save(im, fp, filename): sizes = list(sizes) fp.write(struct.pack(" Date: Sun, 19 Apr 2020 16:45:12 +0300 Subject: [PATCH 2/8] add test for ico append_images --- Tests/test_file_ico.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 9ed1ffcb7..7fb52b12d 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -85,6 +85,24 @@ def test_only_save_relevant_sizes(tmp_path): assert im_saved.info["sizes"] == {(16, 16), (24, 24), (32, 32), (48, 48)} +def test_only_save_append_images(tmp_path): + """append_images should work to provide alternative sizes""" + im = hopper() + provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 255)) + outfile = str(tmp_path / "temp_saved_multi_icon.ico") + im.save(outfile, sizes = [(32, 32), (64, 64)], append_images = [provided_im]) + + with Image.open(outfile) as reread: + reread.size = (64, 64) + reread.load() + assert_image_equal(reread, hopper().resize((64, 64), Image.LANCZOS)) + + with Image.open(outfile) as reread: + reread.size = (32, 32) + reread.load() + assert_image_equal(reread, provided_im) + + def test_unexpected_size(): # This image has been manually hexedited to state that it is 16x32 # while the image within is still 16x16 From 39f47387753d88b3c24743a71907e09245867d01 Mon Sep 17 00:00:00 2001 From: ziplantil Date: Sun, 19 Apr 2020 16:54:53 +0300 Subject: [PATCH 3/8] lint --- Tests/test_file_ico.py | 2 +- src/PIL/IcoImagePlugin.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 7fb52b12d..ba9f84f5e 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -90,7 +90,7 @@ def test_only_save_append_images(tmp_path): im = hopper() provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 255)) outfile = str(tmp_path / "temp_saved_multi_icon.ico") - im.save(outfile, sizes = [(32, 32), (64, 64)], append_images = [provided_im]) + im.save(outfile, sizes=[(32, 32), (64, 64)], append_images=[provided_im]) with Image.open(outfile) as reread: reread.size = (64, 64) diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index 0186751be..62a1fb4ab 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -52,9 +52,7 @@ def _save(im, fp, filename): sizes = list(sizes) fp.write(struct.pack(" Date: Mon, 20 Apr 2020 12:08:41 +0300 Subject: [PATCH 4/8] commit suggestion; update src/PIL/IcoImagePlugin.py Co-Authored-By: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/IcoImagePlugin.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index 62a1fb4ab..dc2d6606a 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -64,9 +64,8 @@ def _save(im, fp, filename): fp.write(struct.pack(" Date: Wed, 4 Nov 2020 22:39:25 +1100 Subject: [PATCH 5/8] Simplified test --- Tests/test_file_ico.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index ba9f84f5e..a5f728c42 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -87,19 +87,15 @@ def test_only_save_relevant_sizes(tmp_path): def test_only_save_append_images(tmp_path): """append_images should work to provide alternative sizes""" - im = hopper() - provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 255)) + im = hopper("RGBA") + provided_im = Image.new("RGBA", (32, 32), (255, 0, 0)) outfile = str(tmp_path / "temp_saved_multi_icon.ico") - im.save(outfile, sizes=[(32, 32), (64, 64)], append_images=[provided_im]) + im.save(outfile, sizes=[(32, 32), (128, 128)], append_images=[provided_im]) with Image.open(outfile) as reread: - reread.size = (64, 64) - reread.load() - assert_image_equal(reread, hopper().resize((64, 64), Image.LANCZOS)) + assert_image_equal(reread, hopper("RGBA")) - with Image.open(outfile) as reread: reread.size = (32, 32) - reread.load() assert_image_equal(reread, provided_im) From 2ae597c357134a25a6c8ec3da669fedee11bd8f9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 4 Nov 2020 22:42:53 +1100 Subject: [PATCH 6/8] Renamed variable to match IcnsImagePlugin --- src/PIL/IcoImagePlugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index dc2d6606a..152d2a275 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -52,7 +52,7 @@ def _save(im, fp, filename): sizes = list(sizes) fp.write(struct.pack(" Date: Wed, 4 Nov 2020 22:52:45 +1100 Subject: [PATCH 7/8] Document ICO append_images [ci skip] --- docs/handbook/image-file-formats.rst | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 40db9fe2b..691892536 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -127,8 +127,8 @@ following options are available:: images in the list can be single or multiframe images. This is currently supported for GIF, PDF, TIFF, and WebP. - It is also supported for ICNS. If images are passed in of relevant sizes, - they will be used instead of scaling down the main image. + It is also supported for ICO and ICNS. If images are passed in of relevant + sizes, they will be used instead of scaling down the main image. **include_color_table** Whether or not to include local color table. @@ -238,6 +238,15 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: (64, 64), (128, 128), (256, 256)]``. Any sizes bigger than the original size or 256 will be ignored. +The :py:meth:`~PIL.Image.Image.save` method can take the following keyword arguments: + +**append_images** + A list of images to replace the scaled down versions of the image. + The order of the images does not matter, as their use is determined by + the size of each image. + + .. versionadded:: 8.1.0 + IM ^^ From 46f7b4a439819d45ab727ec87384d5d651ae26bc Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Thu, 24 Dec 2020 10:50:43 +1100 Subject: [PATCH 8/8] Updated test name and text --- Tests/test_file_ico.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index a5f728c42..34fb42abf 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -85,8 +85,8 @@ def test_only_save_relevant_sizes(tmp_path): assert im_saved.info["sizes"] == {(16, 16), (24, 24), (32, 32), (48, 48)} -def test_only_save_append_images(tmp_path): - """append_images should work to provide alternative sizes""" +def test_save_append_images(tmp_path): + # append_images should be used for scaled down versions of the image im = hopper("RGBA") provided_im = Image.new("RGBA", (32, 32), (255, 0, 0)) outfile = str(tmp_path / "temp_saved_multi_icon.ico")