| import io | |
| from PIL import Image, ImageOps | |
| import warnings | |
| def prepare_database_image(image_bytes: bytes, quality: int = 90) -> bytes: | |
| warnings.simplefilter('ignore', Image.DecompressionBombWarning) | |
| Image.MAX_IMAGE_PIXELS = 25000000 | |
| with Image.open(io.BytesIO(image_bytes)) as img: | |
| img = img.convert("RGBA") | |
| img = ImageOps.fit(img, (400, 600), centering=(0.5, 0.2)) | |
| out_io = io.BytesIO() | |
| img.save(out_io, format="WEBP", quality=quality) | |
| return out_io.getvalue() | |