Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/ico-append-images
Browse files Browse the repository at this point in the history
ICO append images suggestions
  • Loading branch information
ziplantil committed Dec 11, 2020
2 parents 4ca8a35 + 1ced7b7 commit 0722870
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
12 changes: 4 additions & 8 deletions Tests/test_file_ico.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
13 changes: 11 additions & 2 deletions docs/handbook/image-file-formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
^^

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _save(im, fp, filename):
sizes = list(sizes)
fp.write(struct.pack("<H", len(sizes))) # idCount(2)
offset = fp.tell() + len(sizes) * 16
alt_images = {im.size: im for im in im.encoderinfo.get("append_images", [])}
provided_images = {im.size: im for im in im.encoderinfo.get("append_images", [])}
for size in sizes:
width, height = size
# 0 means 256
Expand All @@ -64,7 +64,7 @@ def _save(im, fp, filename):
fp.write(struct.pack("<H", 32)) # wBitCount(2)

image_io = BytesIO()
tmp = alt_images.get(size)
tmp = provided_images.get(size)
if not tmp:
# TODO: invent a more convenient method for proportional scalings
tmp = im.copy()
Expand Down

0 comments on commit 0722870

Please sign in to comment.