File size: 1,222 Bytes
5479120
 
dcadc25
 
 
 
 
5479120
dcadc25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecbbafe
dcadc25
 
 
5479120
 
 
dcadc25
 
 
 
5479120
 
dcadc25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""File to preprocess images"""

import os

from PIL import Image


def get_images(folder: str) -> list:
    """Get images from a folder

    Args:
        folder (str): path to the folder

    Returns:
        images: list of images in the folder
    """
    images = []
    extetntions = [".png", ".jpeg", ".jpg"]
    for filename in os.listdir(folder):
        if any(filename.endswith(ext) for ext in extetntions):
            images.append(os.path.join(folder, filename))
    return images


def resize_image(img_path: str, target_w: int = 200, target_h: int = 200) -> Image.Image:
    """Resize an image

    Args:
        img_path (str): path to the image
        target_w (int): int to resize the width of the image
        target_h (int): int to resize the height of the image

    Returns:
        img: resized images
    """
    img = Image.open(img_path)
    img = img.resize((target_w, target_h))
    return img


def get_image_caption(image_path):
    """Get the caption of an image

    Args:
        image_path (str): path to the image

    Returns:
        image_path: caption of the image
    """
    image_path = image_path.split("/")[-1]
    image_path = image_path.split(".")[0]
    return image_path