-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutilities.py
More file actions
22 lines (19 loc) · 764 Bytes
/
Copy pathutilities.py
File metadata and controls
22 lines (19 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
from PIL import Image
def save_image(image, filename):
image = np.clip(image, 0.0, 255.0)
image = image.astype(np.uint8)
with open(filename, 'wb') as file:
Image.fromarray(image).save(file, 'jpeg')
def normalize_image(image):
image_min = image.min()
image_max = image.max()
return (image - image_min) / (image_max - image_min)
def resize_image(image, size=None, factor=None):
if factor is not None:
size = np.array(image.shape[0:2]) * factor
size = size.astype(int)
size = tuple(reversed(size)) # compensation for the numpy/PIL difference
image = np.clip(image, 0.0, 255.0)
image = Image.fromarray(image.astype(np.uint8))
return np.float32(image.resize(size, Image.LANCZOS))