""" Image utilities for BrightMind AI with shorter naming system """ import os import time import hashlib from datetime import datetime from pathlib import Path def generate_short_image_name(topic, content_type="lesson", index=0): """ Generate a short, meaningful image name. Args: topic (str): The main topic of the image content_type (str): Type of content (lesson, worksheet, etc.) index (int): Index for multiple images of same topic Returns: str: Short image name like 'img_alg_123456_0.png' """ # Clean topic name - remove special chars, limit length clean_topic = ''.join(c for c in topic.lower() if c.isalnum())[:6] # Get timestamp (last 6 digits) timestamp = str(int(time.time()))[-6:] # Create short name short_name = f"img_{clean_topic}_{timestamp}_{index}" return short_name def save_image_with_short_name(image_data, topic, description, content_type="lesson", output_dir="generated_images"): """ Save image with short name and return the filename. Args: image_data (bytes): The actual image data topic (str): The topic of the image description (str): Description of the image content_type (str): Type of content output_dir (str): Directory to save images Returns: tuple: (filename, full_path, metadata) """ # Create output directory if it doesn't exist Path(output_dir).mkdir(exist_ok=True) # Generate short name short_name = generate_short_image_name(topic, content_type) filename = f"{short_name}.png" full_path = os.path.join(output_dir, filename) # Save the image if image_data: with open(full_path, 'wb') as f: f.write(image_data) print(f"šŸ“ø Image saved: {filename}") else: print(f"šŸ“ø Image name generated: {filename} (no data to save)") # Create metadata metadata = { "short_name": short_name, "filename": filename, "full_path": full_path, "topic": topic, "description": description, "content_type": content_type, "timestamp": datetime.now().isoformat(), "size_bytes": len(image_data) if image_data else 0 } return filename, full_path, metadata def create_image_html_tag(image_path, alt_text="Generated image", width=400, height=300): """ Create an HTML img tag for displaying the image. Args: image_path (str): Path to the image file alt_text (str): Alt text for the image width (int): Width of the image height (int): Height of the image Returns: str: HTML img tag """ # Check if image exists if os.path.exists(image_path): return f'{alt_text}' else: # Return placeholder if image doesn't exist return f'
šŸ“· {alt_text}
' def integrate_with_main_app(): """ Example of how to integrate short naming with the main app. """ print("šŸ”§ Integration example for main app:") print(""" # In your main app.py, replace the image generation calls with: from image_utils import save_image_with_short_name, create_image_html_tag # When generating images: def generate_real_image(description, topic, content_type): # ... your existing image generation code ... if image_data: filename, full_path, metadata = save_image_with_short_name( image_data, topic, description, content_type ) # Create HTML tag for display html_tag = create_image_html_tag(full_path, description) return html_tag else: # Fallback placeholder return f'
{description}
' """) def demo_integration(): """ Demo the integration with sample data. """ print("šŸš€ Image Utils Integration Demo") print("=" * 40) # Sample image data (in real app, this would be actual image bytes) sample_topics = [ ("Algebra", "Visual introduction to algebraic concepts", "lesson"), ("Geometry", "Diagram showing geometric shapes", "worksheet"), ("Physics", "Real-world physics examples", "activity") ] for topic, description, content_type in sample_topics: print(f"\nšŸ“š Processing: {topic}") # Generate short name and save (simulated) filename, full_path, metadata = save_image_with_short_name( None, # No actual image data in demo topic, description, content_type ) # Create HTML tag html_tag = create_image_html_tag(full_path, description) print(f" āœ… Generated: {filename}") print(f" šŸ“ Path: {full_path}") print(f" šŸ·ļø HTML: {html_tag[:100]}...") if __name__ == "__main__": demo_integration() print("\n" + "="*50) integrate_with_main_app()