1+ #!/usr/bin/env python3
2+ import struct
3+ import os
4+
5+ def create_ico_from_pngs (png_files , output_file ):
6+ """Create an ICO file from multiple PNG files."""
7+ # ICO header
8+ ico_header = struct .pack ('<HHH' , 0 , 1 , len (png_files ))
9+
10+ # Directory entries and image data
11+ entries = []
12+ images = []
13+ offset = 6 + 16 * len (png_files ) # Header + directory entries
14+
15+ for png_file in png_files :
16+ with open (png_file , 'rb' ) as f :
17+ png_data = f .read ()
18+
19+ # Get size from filename (e.g., favicon-16.png -> 16)
20+ size = int (png_file .split ('-' )[1 ].split ('.' )[0 ])
21+
22+ # ICO directory entry
23+ entry = struct .pack ('<BBBBHHII' ,
24+ size if size < 256 else 0 , # Width (0 = 256)
25+ size if size < 256 else 0 , # Height (0 = 256)
26+ 0 , # Color palette
27+ 0 , # Reserved
28+ 1 , # Color planes
29+ 32 , # Bits per pixel
30+ len (png_data ), # Image size
31+ offset # Image offset
32+ )
33+ entries .append (entry )
34+ images .append (png_data )
35+ offset += len (png_data )
36+
37+ # Write ICO file
38+ with open (output_file , 'wb' ) as f :
39+ f .write (ico_header )
40+ for entry in entries :
41+ f .write (entry )
42+ for image in images :
43+ f .write (image )
44+
45+ # Create the ICO file
46+ png_files = ['favicon-16.png' , 'favicon-32.png' , 'favicon-48.png' ]
47+ create_ico_from_pngs (png_files , 'favicon.ico' )
48+ print ("favicon.ico created successfully!" )
49+
50+ # Clean up temporary PNG files
51+ for png_file in png_files :
52+ os .remove (png_file )
53+ print ("Temporary PNG files removed." )
0 commit comments