Files

34 lines
984 B
Python
Raw Permalink Normal View History

from PIL import Image
import struct
import io
import os
png_path = r'D:\keilProject\HC32F460\Git\GateWay_ConfigureAPP\物联网网关.png'
ico_path = r'D:\keilProject\HC32F460\Git\GateWay_ConfigureAPP\logo.ico'
img = Image.open(png_path).convert('RGBA')
sizes = [16, 32, 48, 64, 128, 256]
ico_data = []
for size in sizes:
resized = img.resize((size, size), Image.Resampling.LANCZOS)
png_bytes = io.BytesIO()
resized.save(png_bytes, format='PNG')
ico_data.append((size, png_bytes.getvalue()))
header = struct.pack('<HHH', 0, 1, len(ico_data))
entries = b''
images = b''
offset = 6 + len(ico_data) * 16
for size, img_data in ico_data:
w = 0 if size >= 256 else size
h = 0 if size >= 256 else size
entries += struct.pack('<BBBBHHII', w, h, 0, 0, 1, 32, len(img_data), offset)
images += img_data
offset += len(img_data)
with open(ico_path, 'wb') as f:
f.write(header + entries + images)
print('ICO created, size:', os.path.getsize(ico_path))