36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
"""Generate kanjin.ico from a solid-colour PNG embedded in an ICO container."""
|
|
import struct
|
|
import zlib
|
|
from pathlib import Path
|
|
|
|
|
|
def _png_chunk(chunk_type, data):
|
|
c = chunk_type + data
|
|
crc = struct.pack(">I", zlib.crc32(c) & 0xFFFFFFFF)
|
|
return struct.pack(">I", len(data)) + c + crc
|
|
|
|
|
|
def solid_png_bytes(width, height, r, g, b):
|
|
header = b"\x89PNG\r\n\x1a\n"
|
|
ihdr = _png_chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
|
|
raw = bytearray()
|
|
for _ in range(height):
|
|
raw.append(0)
|
|
raw.extend(bytes([r, g, b]) * width)
|
|
idat = _png_chunk(b"IDAT", zlib.compress(bytes(raw)))
|
|
iend = _png_chunk(b"IEND", b"")
|
|
return header + ihdr + idat + iend
|
|
|
|
|
|
def make_ico(png_data):
|
|
header = struct.pack("<HHH", 0, 1, 1)
|
|
entry = struct.pack("<BBBBHHII", 0, 0, 0, 0, 1, 32, len(png_data), 22)
|
|
return header + entry + png_data
|
|
|
|
|
|
if __name__ == "__main__":
|
|
dst = Path(__file__).parent / "kanjin.ico"
|
|
png = solid_png_bytes(256, 256, 0x1A, 0x1A, 0x2E)
|
|
dst.write_bytes(make_ico(png))
|
|
print(f"Created {dst.name}")
|