Spaces:
Sleeping
Sleeping
| from fastapi import UploadFile, HTTPException | |
| from PIL import Image | |
| import io | |
| import logging | |
| from typing import Tuple | |
| def validate_image_size(image: Image.Image) -> Tuple[bool, str]: | |
| """Basic image validation""" | |
| try: | |
| # Just verify that we can get the image size | |
| _ = image.size | |
| return True, "" | |
| except Exception as e: | |
| return False, "Invalid image format" | |
| def read_image(upload_file: UploadFile) -> Image.Image: | |
| """Read and validate image from uploaded file""" | |
| try: | |
| # Read image directly | |
| image_bytes = upload_file.file.read() | |
| image = Image.open(io.BytesIO(image_bytes)) | |
| # Convert to RGB if needed | |
| if image.mode not in ('RGB', 'L'): | |
| image = image.convert('RGB') | |
| return image | |
| except IOError as e: | |
| logging.error(f"Failed to read image: {str(e)}") | |
| raise HTTPException(status_code=400, detail="Invalid image format") | |
| except Exception as e: | |
| logging.error(f"Unexpected error reading image: {str(e)}") | |
| raise HTTPException(status_code=500, detail="Failed to process image") |