|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert favicon.png to various formats and sizes with padding removed. |
| 4 | +""" |
| 5 | + |
| 6 | +from PIL import Image |
| 7 | +import io |
| 8 | +import base64 |
| 9 | + |
| 10 | +def trim_whitespace(image): |
| 11 | + """Remove excess whitespace/transparent areas from an image.""" |
| 12 | + # Convert to RGBA if not already |
| 13 | + if image.mode != 'RGBA': |
| 14 | + image = image.convert('RGBA') |
| 15 | + |
| 16 | + # Get bounding box of non-transparent pixels |
| 17 | + bbox = image.getbbox() |
| 18 | + |
| 19 | + if bbox: |
| 20 | + # Crop to content |
| 21 | + image = image.crop(bbox) |
| 22 | + |
| 23 | + return image |
| 24 | + |
| 25 | +def create_icon(source_path, output_path, size, add_padding=True): |
| 26 | + """Create an icon from source image with specified size.""" |
| 27 | + # Open and trim the source image |
| 28 | + img = Image.open(source_path) |
| 29 | + img = trim_whitespace(img) |
| 30 | + |
| 31 | + # Add padding if requested (10% on each side) |
| 32 | + if add_padding: |
| 33 | + padding = int(size * 0.05) # 5% padding on each side |
| 34 | + new_size = size - (padding * 2) |
| 35 | + img.thumbnail((new_size, new_size), Image.Resampling.LANCZOS) |
| 36 | + |
| 37 | + # Create new image with padding |
| 38 | + final_img = Image.new('RGBA', (size, size), (0, 0, 0, 0)) |
| 39 | + offset = ((size - img.width) // 2, (size - img.height) // 2) |
| 40 | + final_img.paste(img, offset, img if img.mode == 'RGBA' else None) |
| 41 | + else: |
| 42 | + img.thumbnail((size, size), Image.Resampling.LANCZOS) |
| 43 | + final_img = img |
| 44 | + |
| 45 | + final_img.save(output_path, 'PNG', optimize=True, compress_level=9) |
| 46 | + print(f"Created {output_path} ({size}x{size})") |
| 47 | + |
| 48 | +def create_svg(source_path, output_path): |
| 49 | + """Create SVG from source image.""" |
| 50 | + # Open and trim the source image |
| 51 | + img = Image.open(source_path) |
| 52 | + img = trim_whitespace(img) |
| 53 | + |
| 54 | + # Save as PNG to base64 with optimization |
| 55 | + temp_png = io.BytesIO() |
| 56 | + img.save(temp_png, 'PNG', optimize=True, compress_level=9) |
| 57 | + img_data = temp_png.getvalue() |
| 58 | + img_base64 = base64.b64encode(img_data).decode('utf-8') |
| 59 | + |
| 60 | + width, height = img.size |
| 61 | + |
| 62 | + svg_content = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}"> |
| 63 | + <image href="data:image/png;base64,{img_base64}" width="{width}" height="{height}"/> |
| 64 | +</svg>''' |
| 65 | + |
| 66 | + with open(output_path, 'w') as f: |
| 67 | + f.write(svg_content) |
| 68 | + |
| 69 | + print(f"Created {output_path}") |
| 70 | + |
| 71 | +def main(): |
| 72 | + base_path = '/Users/user/Documents/03_app/tabitabi/apps/web/static' |
| 73 | + source = f'{base_path}/favicon.png' |
| 74 | + |
| 75 | + # Create trimmed favicon.svg |
| 76 | + create_svg(source, f'{base_path}/favicon.svg') |
| 77 | + |
| 78 | + # Create icon-192.png |
| 79 | + create_icon(source, f'{base_path}/icons/icon-192.png', 192) |
| 80 | + |
| 81 | + # Create icon-512.png |
| 82 | + create_icon(source, f'{base_path}/icons/icon-512.png', 512) |
| 83 | + |
| 84 | + print("\nAll icons created successfully!") |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + main() |
0 commit comments