|
| 1 | +from PIL import Image |
| 2 | +import os |
| 3 | + |
| 4 | +# List of icon sizes for PNG files |
| 5 | +icon_sizes = [ |
| 6 | + (16, 16), |
| 7 | + (32, 32), |
| 8 | + (72, 72), |
| 9 | + (96, 96), |
| 10 | + (128, 128), |
| 11 | + (144, 144), |
| 12 | + (152, 152), |
| 13 | + (180, 180), |
| 14 | + (192, 192), |
| 15 | + (384, 384), |
| 16 | + (512, 512) |
| 17 | +] |
| 18 | + |
| 19 | +def resize_and_save(input_image_path, output_directory, ico_directory): |
| 20 | + # Load the input image |
| 21 | + img = Image.open(input_image_path) |
| 22 | + |
| 23 | + # Ensure the output directory exists |
| 24 | + os.makedirs(output_directory, exist_ok=True) |
| 25 | + |
| 26 | + # Ensure the ICO output directory exists |
| 27 | + os.makedirs(ico_directory, exist_ok=True) |
| 28 | + |
| 29 | + # Variable to hold the 32x32 image for the ICO file |
| 30 | + img_32x32 = None |
| 31 | + |
| 32 | + # Process each size |
| 33 | + for size in icon_sizes: |
| 34 | + resized_img = img.resize(size, Image.LANCZOS) |
| 35 | + output_path = os.path.join(output_directory, f'icon-{size[0]}x{size[1]}.png') |
| 36 | + resized_img.save(output_path) |
| 37 | + print(f'Saved: {output_path}') |
| 38 | + |
| 39 | + # Save the 32x32 image for the ICO file |
| 40 | + if size == (32, 32): |
| 41 | + img_32x32 = resized_img |
| 42 | + |
| 43 | + # Save the ICO file if the 32x32 image was created |
| 44 | + if img_32x32: |
| 45 | + ico_output_path = os.path.join(ico_directory, 'favicon.ico') |
| 46 | + img_32x32.save(ico_output_path, format='ICO') |
| 47 | + print(f'Saved: {ico_output_path}') |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + # Input image path |
| 51 | + input_image_path = 'icon.png' # Replace with your image path |
| 52 | + |
| 53 | + # Output directory to save resized images |
| 54 | + output_directory = 'public/icons' |
| 55 | + |
| 56 | + # Directory to save the ICO file |
| 57 | + ico_directory = 'public' |
| 58 | + |
| 59 | + # Call the function |
| 60 | + resize_and_save(input_image_path, output_directory, ico_directory) |
0 commit comments