#!/usr/bin/env python3 """ Test script to verify view_session functionality """ import os import shutil from pathlib import Path from PIL import Image import io def create_test_session(): """Create a test session with sample images for testing view_session""" print("๐Ÿงช Creating test session for view_session testing...") # Change to cache directory cache_dir = Path(".cache") if not cache_dir.exists(): print("โŒ .cache directory not found. Run 'python app.py' first.") return False os.chdir(cache_dir) # Create Generated directory if it doesn't exist generated_dir = Path("Generated") generated_dir.mkdir(exist_ok=True) # Create test session directory test_timestamp = "20250924_090627" # Same as your example session_dir = generated_dir / f"session_{test_timestamp}" # Remove existing test session if it exists if session_dir.exists(): shutil.rmtree(session_dir) session_dir.mkdir(exist_ok=True) print(f"โœ… Created test session directory: {session_dir}") # Create sample images test_images = [ ("generated_realistic_20250924_090627.jpg", (512, 512, 'red')), ("generated_artistic_20250924_090627.jpg", (512, 512, 'blue')), ("generated_vintage_20250924_090627.jpg", (512, 512, 'green')), ("webcam_input_20250924_090627.jpg", (512, 512, 'yellow')) ] for filename, (width, height, color) in test_images: try: # Create a simple colored image if color == 'red': img = Image.new('RGB', (width, height), (255, 100, 100)) elif color == 'blue': img = Image.new('RGB', (width, height), (100, 100, 255)) elif color == 'green': img = Image.new('RGB', (width, height), (100, 255, 100)) else: # yellow img = Image.new('RGB', (width, height), (255, 255, 100)) # Add some text to identify the image from PIL import ImageDraw, ImageFont draw = ImageDraw.Draw(img) try: # Try to use a better font, fallback to default if not available font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 24) except: font = ImageFont.load_default() style_name = filename.replace('.jpg', '').replace(f'_{test_timestamp}', '').replace('generated_', '').replace('webcam_input', 'Input').title() draw.text((50, 50), f"{style_name}\nTest Image", fill='white', font=font) draw.text((50, 400), f"{filename}", fill='white', font=font) # Save the image img_path = session_dir / filename img.save(img_path, 'JPEG', quality=90) print(f"โœ… Created test image: {filename}") except Exception as e: print(f"โŒ Failed to create {filename}: {e}") print(f"\n๐ŸŽฏ Test session created successfully!") print(f"๐Ÿ“ Session directory: {session_dir.absolute()}") print(f"๐ŸŒ Test URL: http://localhost:7860/view_session/{test_timestamp}") print(f"๐Ÿ“ Files created: {len(list(session_dir.glob('*.jpg')))} images") # Go back to main directory os.chdir("..") return True if __name__ == "__main__": create_test_session()