Spaces:
Runtime error
Runtime error
File size: 2,663 Bytes
0453188 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | """
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
|