Skip to content

Instantly share code, notes, and snippets.

@mmozeiko
Created January 10, 2022 02:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmozeiko/47cf52adc39441d512c28efb2efb3a20 to your computer and use it in GitHub Desktop.
Save mmozeiko/47cf52adc39441d512c28efb2efb3a20 to your computer and use it in GitHub Desktop.
packs multiple images into ico file
#!/usr/bin/env python3
# packs multiple images (bmp/png/...) into ico file
# width and height of images must be <= 256
# pixel format of images must be 32-bit RGBA
import argparse
import struct
import os
from PIL import Image # https://python-pillow.org/
def pack(output, inp):
count = len(inp)
with open(output, "wb") as f:
f.write(struct.pack("<HHH", 0, 1, count))
offset = struct.calcsize("<HHH") + struct.calcsize("<BBBBHHII")*count
for i in inp:
size = os.path.getsize(i)
img = Image.open(i)
w = 0 if img.width == 256 else img.width
h = 0 if img.height == 256 else img.height
if w > 256 or h > 256:
exit("Image max size is 256x256")
f.write(struct.pack("<BBBBHHII", w, h, 0, 0, 0, 32, size, offset))
offset += size
for i in inp:
f.write(open(i, "rb").read())
if __name__ == "__main__":
ap = argparse.ArgumentParser(description="pack multiple images into ico file")
ap.add_argument("-o", "--out", help="output file")
ap.add_argument("input", type=str, nargs='+', help="input images")
args = ap.parse_args()
pack(args.out, args.input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment