File: images2ico.py

package info (click to toggle)
rclone-browser 1.8.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,192 kB
  • sloc: cpp: 4,327; sh: 161; xml: 50; python: 24; makefile: 5
file content (37 lines) | stat: -rwxr-xr-x 1,092 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3

# to install "pip3 install Pillow"

# 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.stat(i).st_size
      img = Image.open(i)
      w = 0 if img.width == 256 else img.width
      h = 0 if img.height == 256 else img.height
      f.write(struct.pack("BBBBHHII", w, h, 0, 0, 1, 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)