Merge pull request #4568 from ziplantil/ico-append-images

Add append_images support for ICO
This commit is contained in:
Andrew Murray 2020-12-24 11:10:33 +11:00 committed by GitHub
commit e37a8a263d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 5 deletions

View File

@ -86,6 +86,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

View File

@ -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, PNG, 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
^^

View File

@ -54,6 +54,7 @@ def _save(im, fp, filename):
sizes = list(sizes)
fp.write(struct.pack("<H", len(sizes))) # idCount(2)
offset = fp.tell() + len(sizes) * 16
provided_images = {im.size: im for im in im.encoderinfo.get("append_images", [])}
for size in sizes:
width, height = size
# 0 means 256
@ -65,6 +66,8 @@ def _save(im, fp, filename):
fp.write(struct.pack("<H", 32)) # wBitCount(2)
image_io = BytesIO()
tmp = provided_images.get(size)
if not tmp:
# TODO: invent a more convenient method for proportional scalings
tmp = im.copy()
tmp.thumbnail(size, Image.LANCZOS, reducing_gap=None)