| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import argparse |
| 10 |
import struct |
| 11 |
import os |
| 12 |
from PIL import Image |
| 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) |