-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_utils.py
30 lines (26 loc) · 1.05 KB
/
image_utils.py
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
import os
from PIL import Image
import numpy as np
def folder_to_map(folder_path, dictionary, save_function, depth=1):
depth = depth-1
subfolders = os.listdir(folder_path)
for subfolder in subfolders:
subfolder_path = os.path.join(folder_path, subfolder)
if depth == 0:
dictionary[subfolder_path] = save_function(subfolder_path)
else:
folder_to_map(subfolder_path, dictionary, save_function,depth)
def open_image(image_path, size):
return Image.open(image_path).resize((size,size)).convert('RGB')
def load_image(image_path, use_memory, size, dictionary=None):
if use_memory:
return dictionary[image_path]
else:
return open_image(image_path, size)
def interval_mapping(image, from_min, from_max, to_min, to_max):
# map values from [from_min, from_max] to [to_min, to_max]
# image: input array
from_range = from_max - from_min
to_range = to_max - to_min
scaled = np.array((image - from_min) / float(from_range), dtype=np.float32)
return to_min + (scaled * to_range)