Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """ | |
| Generate PWA icons for BattleWords. | |
| Creates 192x192 and 512x512 icons with ocean theme and 'BW' text. | |
| """ | |
| from PIL import Image, ImageDraw, ImageFont | |
| import os | |
| def create_icon(size, filename): | |
| """Create a square icon with ocean gradient background and 'BW' text.""" | |
| # Create image with ocean blue gradient | |
| img = Image.new('RGB', (size, size)) | |
| draw = ImageDraw.Draw(img) | |
| # Draw vertical gradient (ocean theme) | |
| water_sky = (29, 100, 200) # #1d64c8 | |
| water_deep = (11, 42, 74) # #0b2a4a | |
| for y in range(size): | |
| # Interpolate between sky and deep | |
| ratio = y / size | |
| r = int(water_sky[0] * (1 - ratio) + water_deep[0] * ratio) | |
| g = int(water_sky[1] * (1 - ratio) + water_deep[1] * ratio) | |
| b = int(water_sky[2] * (1 - ratio) + water_deep[2] * ratio) | |
| draw.rectangle([(0, y), (size, y + 1)], fill=(r, g, b)) | |
| # Draw circular background for better icon appearance | |
| circle_margin = size // 10 | |
| circle_bbox = [circle_margin, circle_margin, size - circle_margin, size - circle_margin] | |
| # Draw white circle with transparency | |
| overlay = Image.new('RGBA', (size, size), (255, 255, 255, 0)) | |
| overlay_draw = ImageDraw.Draw(overlay) | |
| overlay_draw.ellipse(circle_bbox, fill=(255, 255, 255, 40)) | |
| # Composite the overlay | |
| img = img.convert('RGBA') | |
| img = Image.alpha_composite(img, overlay) | |
| # Draw 'BW' text | |
| font_size = size // 3 | |
| try: | |
| # Try to load a nice bold font | |
| font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size) | |
| except Exception: | |
| try: | |
| # Fallback for Windows | |
| font = ImageFont.truetype("C:/Windows/Fonts/arialbd.ttf", font_size) | |
| except Exception: | |
| # Ultimate fallback | |
| font = ImageFont.load_default() | |
| draw = ImageDraw.Draw(img) | |
| text = "BW" | |
| # Get text bounding box for centering | |
| bbox = draw.textbbox((0, 0), text, font=font) | |
| text_width = bbox[2] - bbox[0] | |
| text_height = bbox[3] - bbox[1] | |
| # Center the text | |
| x = (size - text_width) // 2 | |
| y = (size - text_height) // 2 - (bbox[1] // 2) | |
| # Draw text with shadow for depth | |
| shadow_offset = size // 50 | |
| draw.text((x + shadow_offset, y + shadow_offset), text, fill=(0, 0, 0, 100), font=font) | |
| draw.text((x, y), text, fill='white', font=font) | |
| # Convert back to RGB for saving as PNG | |
| if img.mode == 'RGBA': | |
| background = Image.new('RGB', img.size, (11, 42, 74)) | |
| background.paste(img, mask=img.split()[3]) # Use alpha channel as mask | |
| img = background | |
| # Save | |
| img.save(filename, 'PNG', optimize=True) | |
| print(f"[OK] Created {filename} ({size}x{size})") | |
| def main(): | |
| """Generate both icon sizes.""" | |
| script_dir = os.path.dirname(os.path.abspath(__file__)) | |
| static_dir = os.path.join(script_dir, 'battlewords', 'static') | |
| # Ensure directory exists | |
| os.makedirs(static_dir, exist_ok=True) | |
| # Generate icons | |
| print("Generating PWA icons for BattleWords...") | |
| create_icon(192, os.path.join(static_dir, 'icon-192.png')) | |
| create_icon(512, os.path.join(static_dir, 'icon-512.png')) | |
| print("\n[SUCCESS] PWA icons generated successfully!") | |
| print(f" Location: {static_dir}") | |
| if __name__ == '__main__': | |
| main() | |