-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnii2png.py
108 lines (90 loc) · 3.75 KB
/
nii2png.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
import scipy, numpy, shutil, os, nibabel
import sys, getopt
import imageio
def niiConvert(inputfile, outputdirectory):
# print('Input file is ', inputfile)
# print('Output folder is ', outputdirectory)
fileNames = []
# set fn as your 4d nifti file
image_array = nibabel.load(inputfile).get_data()
# print(len(image_array.shape))
# if 4D image inputted
if len(image_array.shape) == 4:
# set 4d array dimension values
nx, ny, nz, nw = image_array.shape
# set destination folder
if not os.path.exists(outputdirectory):
os.makedirs(outputdirectory)
# print("Created ouput directory: " + outputdirectory)
# print('Reading NIfTI file...')
total_volumes = image_array.shape[3]
total_slices = image_array.shape[2]
# iterate through volumes
for current_volume in range(0, total_volumes):
slice_counter = 0
# iterate through slices
for current_slice in range(0, total_slices):
if (slice_counter % 1) == 0:
# rotate or no rotate
data = image_array[:, :, current_slice, current_volume]
# alternate slices and save as png
# print('Saving image...')
image_name = (
inputfile[:-4]
+ "_t"
+ "{:0>3}".format(str(current_volume + 1))
+ "_z"
+ "{:0>3}".format(str(current_slice + 1))
+ ".png"
)
imageio.imwrite(image_name, data)
# print('Saved.')
# move images to folder
# print('Moving files...')
src = image_name
shutil.move(src, outputdirectory)
fileNames.append(os.path.basename(src))
slice_counter += 1
# print('Moved.')
# print('Finished converting images')
# else if 3D image inputted
elif len(image_array.shape) == 3:
# set 4d array dimension values
nx, ny, nz = image_array.shape
# set destination folder
if not os.path.exists(outputdirectory):
os.makedirs(outputdirectory)
# print("Created ouput directory: " + outputdirectory)
# print('Reading NIfTI file...')
total_slices = image_array.shape[2]
slice_counter = 0
# iterate through slices
for current_slice in range(0, total_slices):
# alternate slices
if (slice_counter % 1) == 0:
# rotate or no rotate
data = image_array[:, :, current_slice]
# alternate slices and save as png
if (slice_counter % 1) == 0:
# print('Saving image...')
image_name = (
inputfile[:-4]
+ "_z"
+ "{:0>3}".format(str(current_slice + 1))
+ ".png"
)
imageio.imwrite(image_name, data)
# print('Saved.')
# move images to folder
# print('Moving image...')
src = image_name
src_folder = os.path.dirname(os.path.abspath(src))
if not src_folder == outputdirectory:
shutil.move(src, outputdirectory)
fileNames.append(os.path.basename(src))
slice_counter += 1
# print('Moved.')
# print('Finished converting images')
# else:
# print('Not a 3D or 4D Image. Please try again.')
return fileNames