Debian Codegraph @

scripts/images2ico.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # to install "pip3 install Pillow"
4
5 # packs multiple images (bmp/png/...) into ico file
6 # width and height of images must be <= 256
7 # pixel format of images must be 32-bit RGBA
8
9 import argparse
10 import struct
11 import os
12 from PIL import Image # https://python-pillow.org/
13
14 def pack(output, inp):
15 count = len(inp)
16
17 with open(output, "wb") as f:
18 f.write(struct.pack("HHH", 0, 1, count))
19 offset = struct.calcsize("HHH") + struct.calcsize("BBBBHHII")*count
20
21 for i in inp:
22 size = os.stat(i).st_size
23 img = Image.open(i)
24 w = 0 if img.width == 256 else img.width
25 h = 0 if img.height == 256 else img.height
26 f.write(struct.pack("BBBBHHII", w, h, 0, 0, 1, 32, size, offset))
27 offset += size
28
29 for i in inp:
30 f.write(open(i, "rb").read())
31
32 if __name__ == "__main__":
33 ap = argparse.ArgumentParser(description="pack multiple images into ico file")
34 ap.add_argument("-o", "--out", help="output file")
35 ap.add_argument("input", type=str, nargs='+', help="input images")
36 args = ap.parse_args()
37 pack(args.out, args.input)