| 123456789101112131415161718192021 |
- import struct, os, sys
- def _png_size(data):
- # PNG IHDR chunk at offset 16: 4 bytes width, 4 bytes height
- return struct.unpack_from('>II', data, 16)
- def png_to_ico(png_path, ico_path):
- with open(png_path, 'rb') as f:
- png_data = f.read()
- w, h = _png_size(png_data)
- png_data = png_data # full PNG data as icon payload
- header = struct.pack('<HHH', 0, 1, 1)
- entry = struct.pack('<BBBBHHII',
- w if w < 256 else 0,
- h if h < 256 else 0,
- 0, 0, 1, 32, len(png_data), 22)
- with open(ico_path, 'wb') as f:
- f.write(header + entry + png_data)
- if __name__ == '__main__':
- png_to_ico(sys.argv[1], sys.argv[2])
|