File size: 723 Bytes
42b209f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
import numpy as np
from PIL import Image


def load_image(file):
    image = Image.open(file)

    # Convert everything to RGB
    image = image.convert("RGB")

    return image


def resize_image(image, resolution):
    width, height = image.size

    # Clamp resolution
    resolution = max(40, min(300, resolution))  

    aspect_ratio = width / height

    if width >= height:
        new_width = resolution
        new_height = int(resolution / aspect_ratio)
    else:
        new_height = resolution
        new_width = int(resolution * aspect_ratio)

    resized = image.resize(
        (new_width, new_height),
        Image.LANCZOS
    )

    return resized


def image_to_array(image):

    return np.array(image)