| import os
|
| from PIL import Image
|
| import io
|
|
|
| def validate_image(file_content: bytes) -> Image.Image:
|
| """Validate and return a PIL Image."""
|
| try:
|
|
|
| image = Image.open(io.BytesIO(file_content))
|
| image.verify()
|
|
|
|
|
| image = Image.open(io.BytesIO(file_content))
|
|
|
|
|
| if image.mode in ('RGBA', 'LA') or (image.mode != 'RGB'):
|
| image = image.convert('RGB')
|
|
|
| return image
|
| except Exception as e:
|
|
|
| raise ValueError(f"Invalid image file: {str(e)}")
|
|
|
| def save_temp_image(image: Image.Image, path: str) -> str:
|
| """Save image to temporary location."""
|
| try:
|
|
|
| os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
|
| image.save(path)
|
| return path
|
| except Exception as e:
|
|
|
| raise RuntimeError(f"Error saving image to {path}: {str(e)}")
|
|
|