add append_images support for ico, much like icns

This commit is contained in:
ziplantil 2020-04-19 16:29:36 +03:00
parent a0d8e550ec
commit 07b24c6e6e

View File

@ -52,6 +52,9 @@ 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", [])
}
for size in sizes:
width, height = size
# 0 means 256
@ -63,9 +66,12 @@ def _save(im, fp, filename):
fp.write(struct.pack("<H", 32)) # wBitCount(2)
image_io = BytesIO()
# TODO: invent a more convenient method for proportional scalings
tmp = im.copy()
tmp.thumbnail(size, Image.LANCZOS, reducing_gap=None)
if size in alt_images:
tmp = alt_images[size]
else:
# TODO: invent a more convenient method for proportional scalings
tmp = im.copy()
tmp.thumbnail(size, Image.LANCZOS, reducing_gap=None)
tmp.save(image_io, "png")
image_io.seek(0)
image_bytes = image_io.read()