-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
157 lines (95 loc) · 3.37 KB
/
util.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 4 11:58:56 2020
@author: ethan
"""
import os
import numpy as np
from PIL import Image
from libtiff import TIFF
import glob
import cv2
import matplotlib.pyplot as plt
def save_to_tif(path, data):
with open(path, 'wb') as f:
np.save(f, data, allow_pickle=True)
def load_set(folder, is_mask, shuffle=False):
data = []
img_list = sorted(glob.glob(os.path.join(folder, '*.tif')) +
glob.glob(os.path.join(folder, '*.jpg'))+
glob.glob(os.path.join(folder, '*.png'))+
glob.glob(os.path.join(folder, '*.tiff')))
if shuffle:
np.random.shuffle(img_list)
for img_fn in img_list:
img = load_image(img_fn, is_mask)
data.append(img)
return data, img_list
def load_image(path,is_mask):
if not is_mask:
return np.asarray(Image.open(path).convert("RGB"))
else:
return np.asarray(Image.open(path).convert('L'))
def load_set_txt(folder):
data = []
txt_list = sorted(glob.glob(os.path.join(folder, '*.txt')) +
glob.glob(os.path.join(folder, '*.csv')))
for txt_fn in txt_list:
txt = load_txt(txt_fn)
data.append(txt)
return data, txt_list
def load_txt(path):
return np.loadtxt(path)
def create_dir(dirname):
try:
os.mkdir(dirname)
return True
except OSError:
return False
def to_numpy(tensor):
return tensor.detach().cpu().numpy()
def resize_my_images(src, dst):
#credits: https://evigio.com/post/resizing-images-into-squares-with-opencv-and-python
i = 1
img_size = 512
path = src
for img_name in sorted(os.listdir(path)):
img = None
print(img_name)
img = cv2.imread(os.path.join(path, img_name),
cv2.IMREAD_GRAYSCALE)
h, w = img.shape[:2]
a1 = w/h
a2 = h/w
if(a1 > a2):
w_target = round(img_size * a1)
h_target = img_size
r_img = cv2.resize(
img, (w_target, h_target), interpolation=cv2.INTER_AREA)
margin = int(r_img.shape[1] / 6)
crop_img = r_img[0:img_size, margin:(margin+img_size)]
elif(a1 < a2):
w_target = img_size
h_target = round(img_size * a2)
r_img = cv2.resize(img, (w_target, h_target),
interpolation=cv2.INTER_AREA)
margin = int(r_img.shape[0] / 6)
crop_img = r_img[margin:(margin+img_size), 0:img_size]
elif(a1 == a2):
w_target = img_size
h_target = img_size
r_img = cv2.resize(img, (w_target, h_target),
interpolation=cv2.INTER_AREA)
crop_img = r_img[0:img_size, 0:img_size]
if(crop_img.shape[0] != img_size or crop_img.shape[1] != img_size):
crop_img = r_img[0:img_size, 0:img_size]
if(crop_img.shape[0] == img_size and crop_img.shape[1] == img_size):
cv2.imwrite(dst + img_name, crop_img)
i += 1
def tif_to_nparray(img):
tif=TIFF.open(img)
return tif.read_image()
def size(path):
teut=Image.open(path)
width, height = teut.size
return width,height