""" Utility functions for the PawPrint Flyer Generator. """ import os from pathlib import Path from PIL import Image def validate_inputs(animal_photo, animal_name, species): """ Validate user inputs before processing. Args: animal_photo: PIL Image or None animal_name: str species: str Returns: str: Error message if validation fails, None if all OK """ if animal_photo is None: return "⚠️ Please upload an animal photo to continue." if not animal_name or animal_name.strip() == "": return "⚠️ Please enter the animal's name." if not species or species not in ["Dog", "Cat"]: return "⚠️ Please select a species (Dog or Cat)." return None def get_background_options(): """ Get available background images from the assets folder. Returns: dict: Mapping of display names to file paths """ backgrounds_dir = Path(__file__).parent.parent / "assets" / "backgrounds" if not backgrounds_dir.exists(): return {} background_files = { "Cozy Living Room": "cozy_living_room.jpg", "Home with Yard": "home_yard.jpg", "Beach Shore": "beach_shore.jpg", "Autumn Cottage": "autumn_cottage.jpg", "Lavender Field": "lavender_field.jpg", } result = {} for name, filename in background_files.items(): path = backgrounds_dir / filename if path.exists(): result[name] = str(path) return result def resize_image(image, target_size, maintain_aspect=True): """ Resize an image to target size. Args: image: PIL Image target_size: tuple of (width, height) maintain_aspect: bool, whether to maintain aspect ratio Returns: PIL Image: Resized image """ if maintain_aspect: image.thumbnail(target_size, Image.Resampling.LANCZOS) return image else: return image.resize(target_size, Image.Resampling.LANCZOS) def ensure_rgb(image): """ Convert image to RGB mode if needed. Args: image: PIL Image Returns: PIL Image in RGB mode """ if image.mode == 'RGBA': # Create a white background background = Image.new('RGB', image.size, (255, 255, 255)) background.paste(image, mask=image.split()[3]) # Use alpha channel as mask return background elif image.mode != 'RGB': return image.convert('RGB') return image