"""
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''
else:
# Return placeholder if image doesn't exist
return f'