import os from PIL import Image UPLOAD_DIR = os.getenv("UPLOAD_DIR", "uploads") def save_and_compress_images(files, max_photos=5, max_size=(1080, 1080), quality=70): """ Save and compress uploaded images. - max_photos: hard cap on number of photos per listing - max_size: resize images to fit within this resolution - quality: JPEG quality (lower = smaller file) """ os.makedirs(UPLOAD_DIR, exist_ok=True) file_paths = [] for idx, file in enumerate(files[:max_photos]): # enforce photo cap try: img = Image.open(file) img.thumbnail(max_size) # resize to max dimension filename = f"listing_{idx}.jpg" filepath = os.path.join(UPLOAD_DIR, filename) img.convert("RGB").save(filepath, "JPEG", quality=quality) file_paths.append(filepath) except Exception as e: print(f"[Image Error] {e}") return file_paths