Spaces:
Sleeping
Sleeping
| import os | |
| import wave | |
| import math | |
| import struct | |
| from PIL import Image, ImageDraw, ImageFont | |
| # Ensure static directory exists | |
| if not os.path.exists('static'): | |
| os.makedirs('static') | |
| # 1. Generate Audio (WAV) | |
| def generate_audio(filename, duration=10, sample_rate=44100): | |
| print(f"Generating {filename}...") | |
| num_samples = duration * sample_rate | |
| with wave.open(filename, 'w') as wav_file: | |
| wav_file.setnchannels(1) # Mono | |
| wav_file.setsampwidth(2) # 2 bytes per sample (16-bit) | |
| wav_file.setframerate(sample_rate) | |
| for i in range(num_samples): | |
| t = i / sample_rate | |
| # Frequency sweep from 200 to 400 | |
| freq = 200 + (t / duration) * 200 | |
| value = int(32767.0 * math.sin(2 * math.pi * freq * t) * 0.5) | |
| data = struct.pack('<h', value) | |
| wav_file.writeframesraw(data) | |
| generate_audio('static/demo.wav') | |
| # 2. Generate Background Image (JPG) | |
| def generate_bg(filename, width=1920, height=1080): | |
| print(f"Generating {filename}...") | |
| img = Image.new('RGB', (width, height), color='#1e1e2e') | |
| draw = ImageDraw.Draw(img) | |
| # Draw some gradient-like circles | |
| for i in range(10): | |
| x = (i * 200) % width | |
| y = (i * 150) % height | |
| r = 300 | |
| color = ( | |
| int(50 + i * 20), | |
| int(100 + i * 10), | |
| int(200 - i * 10) | |
| ) | |
| draw.ellipse([x-r, y-r, x+r, y+r], fill=color, outline=None) | |
| img.save(filename, quality=90) | |
| generate_bg('static/demo-bg.jpg') | |
| # 3. Generate Logo/Watermark (PNG) | |
| def generate_logo(filename, text="DEMO LOGO"): | |
| print(f"Generating {filename}...") | |
| # Create transparent image | |
| img = Image.new('RGBA', (400, 150), (0, 0, 0, 0)) | |
| draw = ImageDraw.Draw(img) | |
| # Try to load a font, fallback to default | |
| font = None | |
| try: | |
| # Common Linux/Docker path | |
| font_path = "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf" | |
| if os.path.exists(font_path): | |
| font = ImageFont.truetype(font_path, 60) | |
| else: | |
| # MacOS path | |
| font_path = "/System/Library/Fonts/Helvetica.ttc" | |
| if os.path.exists(font_path): | |
| font = ImageFont.truetype(font_path, 80) | |
| else: | |
| # Try loading by name (works on some systems) | |
| font = ImageFont.truetype("Arial.ttf", 60) | |
| except: | |
| pass | |
| if font is None: | |
| font = ImageFont.load_default() | |
| # Draw White Text | |
| draw.text((20, 20), text, font=font, fill=(255, 255, 255, 255)) | |
| img.save(filename) | |
| generate_logo('static/demo-logo.png') | |
| print("Assets generated.") | |