Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python | |
| from __future__ import annotations | |
| import PIL.Image | |
| def cropImage(image: PIL.Image.Image): | |
| original_width, original_height = image.size | |
| scale = max(original_width, original_height) / min(original_width, original_height) | |
| target_width = 512 | |
| target_height = 768 | |
| if scale < 1.1: | |
| target_width = 640 | |
| target_height = 640 | |
| elif original_width > original_height: | |
| target_width = 768 | |
| target_height = 512 | |
| if original_width / original_height > target_width / target_height: | |
| new_width = int(original_height * (target_width / target_height)) | |
| crop_box = ((original_width - new_width) // 2, 0, (original_width + new_width) // 2, original_height) | |
| else: | |
| new_height = int(original_width * (target_height / target_width)) | |
| crop_box = (0, (original_height - new_height) // 2, original_width, (original_height + new_height) // 2) | |
| cropped_image = image.convert("RGB") | |
| cropped_image = cropped_image.crop(crop_box) | |
| cropped_image = cropped_image.resize((target_width, target_height)) | |
| return cropped_image | |