-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
26 lines (20 loc) · 764 Bytes
/
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
import numpy as np
from PIL import Image
import cv2
import torch
import torchvision
import torch.nn.functional as F
import torchvision.transforms.functional as T
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg"])
def load_img(filepath):
img = Image.open(filepath).convert('RGB')
img = img.resize((256, 256), Image.BICUBIC)
return img
def save_img(image_tensor, filename):
image_numpy = image_tensor.float().numpy()
image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0
image_numpy = image_numpy.clip(0, 255)
image_numpy = image_numpy.astype(np.uint8)
cv2.imwrite(filename, image_numpy)
print("Image saved as {}".format(filename),end='\r')