Spaces:
Sleeping
Sleeping
feat: add image preprocessing to get image path and resize images
Browse files- front/image_preprocessing.py +49 -0
front/image_preprocessing.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
|
6 |
+
def get_images(folder):
|
7 |
+
"""Get images from a folder
|
8 |
+
|
9 |
+
Args:
|
10 |
+
folder (str): path to the folder
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
images: list of images in the folder
|
14 |
+
"""
|
15 |
+
images = []
|
16 |
+
extetntions = [".png", ".jpeg", ".jpg"]
|
17 |
+
for filename in os.listdir(folder):
|
18 |
+
if any(filename.endswith(ext) for ext in extetntions):
|
19 |
+
images.append(os.path.join(folder, filename))
|
20 |
+
return images
|
21 |
+
|
22 |
+
|
23 |
+
def resize_image(image_path, target_height):
|
24 |
+
"""Resize an image
|
25 |
+
|
26 |
+
Args:
|
27 |
+
image_path (str): path to the image
|
28 |
+
target_height (int): int to resize the height of the image
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
img: resized images
|
32 |
+
"""
|
33 |
+
img = Image.open(image_path)
|
34 |
+
img = img.resize((200, target_height))
|
35 |
+
return img
|
36 |
+
|
37 |
+
|
38 |
+
def get_image_caption(image_path):
|
39 |
+
"""Get the caption of an image
|
40 |
+
|
41 |
+
Args:
|
42 |
+
image_path (str): path to the image
|
43 |
+
|
44 |
+
Returns:
|
45 |
+
image_path: caption of the image
|
46 |
+
"""
|
47 |
+
image_path = image_path.split("/")[-1]
|
48 |
+
image_path = image_path.split(".")[0]
|
49 |
+
return image_path
|