diff --git a/Tests/test_file_ico.py b/Tests/test_file_ico.py index 9ed1ffcb70e..34fb42abf4d 100644 --- a/Tests/test_file_ico.py +++ b/Tests/test_file_ico.py @@ -85,6 +85,20 @@ def test_only_save_relevant_sizes(tmp_path): assert im_saved.info["sizes"] == {(16, 16), (24, 24), (32, 32), (48, 48)} +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") + im.save(outfile, sizes=[(32, 32), (128, 128)], append_images=[provided_im]) + + with Image.open(outfile) as reread: + assert_image_equal(reread, hopper("RGBA")) + + reread.size = (32, 32) + 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 diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 40db9fe2b5d..691892536f9 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 ^^ diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index e4a74321b6e..152d2a27561 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -52,6 +52,7 @@ def _save(im, fp, filename): sizes = list(sizes) fp.write(struct.pack("