-
Notifications
You must be signed in to change notification settings - Fork 11
/
images_prepare.py
64 lines (34 loc) · 1.19 KB
/
images_prepare.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
# -*- coding: utf-8 -*-
"""
@author: serdarhelli
"""
import os
import numpy as np
from PIL import Image
from zipfile import ZipFile
from natsort import natsorted
def convert_one_channel(img):
#some images have 3 channels , although they are grayscale image
if len(img.shape)>2:
img=img[:,:,0]
return img
else:
return img
def pre_images(resize_shape,path,include_zip):
if include_zip==True:
ZipFile(path+"/DentalPanoramicXrays.zip").extractall(path)
path=path+'/Images/'
dirs=natsorted(os.listdir(path))
sizes=np.zeros([len(dirs),2])
images=img=Image.open(path+dirs[0])
sizes[0,:]=images.size
images=(images.resize((resize_shape),Image.ANTIALIAS))
images=convert_one_channel(np.asarray(images))
for i in range (1,len(dirs)):
img=Image.open(path+dirs[i])
sizes[i,:]=img.size
img=img.resize((resize_shape),Image.ANTIALIAS)
img=convert_one_channel(np.asarray(img))
images=np.concatenate((images,img))
images=np.reshape(images,(len(dirs),resize_shape[0],resize_shape[1],1))
return images,sizes