File size: 1,069 Bytes
166cd36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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.crop(crop_box)

    cropped_image = cropped_image.resize((target_width, target_height))

    return cropped_image