-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
57 lines (41 loc) · 1.35 KB
/
tools.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import time
import numpy as np
from libtiff import TIFF
import cv2
def get_file_list(file_dir, all_data=False, suffix=['jpg', 'jpeg', 'JPG', 'JPEG', 'png']):
if not os.path.exists(file_dir):
print('path {} is not exist'.format(file_dir))
return []
img_list = []
for root, sdirs, files in os.walk(file_dir):
if not files:
continue
for filename in files:
filepath = os.path.join(root, filename)
if all_data or filename.split('.')[-1] in suffix:
img_list.append(filepath)
return img_list
def get_data(data_path, norm=0):
suffix = data_path.split('/')[-1].split('.')[-1]
if suffix == 'npy':
data = np.load(data_path)
elif suffix == 'tif':
data = TIFF.open(data_path, mode = "r")
data = list(data.iter_images())[0]
else:
data = cv2.imread(data_path)
if norm:
min_v = data.min()
max_v = data.max()
data = (data - min_v) / (max_v - min_v) * norm
#print('data: ', data.min(), data.max())
return data.astype(np.float32)
def save_data(save_dir, data_name, data):
save_path = os.path.join(save_dir, data_name + '.jpg')
cv2.imwrite(save_path, data)
def norm_data(data):
min_v = data.min()
max_v = data.max()
data = (data - min_v) / (max_v - min_v)
return data