Merge pull request #1 from radarhere/ico-append-images

ICO append images suggestions
This commit is contained in:
ziplantil 2020-12-11 12:28:00 +02:00 committed by GitHub
commit 0722870564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 12 deletions

View File

@ -87,19 +87,15 @@ def test_only_save_relevant_sizes(tmp_path):
def test_only_save_append_images(tmp_path): def test_only_save_append_images(tmp_path):
"""append_images should work to provide alternative sizes""" """append_images should work to provide alternative sizes"""
im = hopper() im = hopper("RGBA")
provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 255)) provided_im = Image.new("RGBA", (32, 32), (255, 0, 0))
outfile = str(tmp_path / "temp_saved_multi_icon.ico") 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: with Image.open(outfile) as reread:
reread.size = (64, 64) assert_image_equal(reread, hopper("RGBA"))
reread.load()
assert_image_equal(reread, hopper().resize((64, 64), Image.LANCZOS))
with Image.open(outfile) as reread:
reread.size = (32, 32) reread.size = (32, 32)
reread.load()
assert_image_equal(reread, provided_im) assert_image_equal(reread, provided_im)

View File

@ -127,8 +127,8 @@ following options are available::
images in the list can be single or multiframe images. images in the list can be single or multiframe images.
This is currently supported for GIF, PDF, TIFF, and WebP. This is currently supported for GIF, PDF, TIFF, and WebP.
It is also supported for ICNS. If images are passed in of relevant sizes, It is also supported for ICO and ICNS. If images are passed in of relevant
they will be used instead of scaling down the main image. sizes, they will be used instead of scaling down the main image.
**include_color_table** **include_color_table**
Whether or not to include local 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 (64, 64), (128, 128), (256, 256)]``. Any sizes bigger than the original
size or 256 will be ignored. 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 IM
^^ ^^

View File

@ -52,7 +52,7 @@ def _save(im, fp, filename):
sizes = list(sizes) sizes = list(sizes)
fp.write(struct.pack("<H", len(sizes))) # idCount(2) fp.write(struct.pack("<H", len(sizes))) # idCount(2)
offset = fp.tell() + len(sizes) * 16 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: for size in sizes:
width, height = size width, height = size
# 0 means 256 # 0 means 256
@ -64,7 +64,7 @@ def _save(im, fp, filename):
fp.write(struct.pack("<H", 32)) # wBitCount(2) fp.write(struct.pack("<H", 32)) # wBitCount(2)
image_io = BytesIO() image_io = BytesIO()
tmp = alt_images.get(size) tmp = provided_images.get(size)
if not tmp: if not tmp:
# TODO: invent a more convenient method for proportional scalings # TODO: invent a more convenient method for proportional scalings
tmp = im.copy() tmp = im.copy()