forked from danishm/mritopng
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mritopng.py
107 lines (82 loc) · 3.42 KB
/
mritopng.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
import os
import png
import dicom
import argparse
def mri_to_png(mri_file, png_file):
""" Function to convert from a DICOM image to png
@param mri_file: An opened file like object to read te dicom data
@param png_file: An opened file like object to write the png data
"""
# Extracting data from the mri file
plan = dicom.read_file(mri_file)
shape = plan.pixel_array.shape
image_2d = []
max_val = 0
for row in plan.pixel_array:
pixels = []
for col in row:
pixels.append(col)
if col > max_val: max_val = col
image_2d.append(pixels)
# Rescaling grey scale between 0-255
image_2d_scaled = []
for row in image_2d:
row_scaled = []
for col in row:
col_scaled = int((float(col) / float(max_val)) * 255.0)
row_scaled.append(col_scaled)
image_2d_scaled.append(row_scaled)
# Writing the PNG file
w = png.Writer(shape[0], shape[1], greyscale=True)
w.write(png_file, image_2d_scaled)
def convert_file(mri_file_path, png_file_path):
""" Function to convert an MRI binary file to a
PNG image file.
@param mri_file_path: Full path to the mri file
@param png_file_path: Fill path to the png file
"""
# Making sure that the mri file exists
if not os.path.exists(mri_file_path):
raise Exception('File "%s" does not exists' % mri_file_path)
# Making sure the png file does not exist
if os.path.exists(png_file_path):
raise Exception('File "%s" already exists' % png_file_path)
mri_file = open(mri_file_path, 'rb')
png_file = open(png_file_path, 'wb')
mri_to_png(mri_file, png_file)
png_file.close()
def convert_folder(mri_folder, png_folder):
""" Convert all MRI files in a folder to png files
in a destination folder
"""
# Create the folder for the pnd directory structure
os.makedirs(png_folder)
# Recursively traverse all sub-folders in the path
for mri_sub_folder, subdirs, files in os.walk(mri_folder):
for mri_file in os.listdir(mri_sub_folder):
mri_file_path = os.path.join(mri_sub_folder, mri_file)
# Make sure path is an actual file
if os.path.isfile(mri_file_path):
# Replicate the original file structure
rel_path = os.path.relpath(mri_sub_folder, mri_folder)
png_folder_path = os.path.join(png_folder, rel_path)
if not os.path.exists(png_folder_path):
os.makedirs(png_folder_path)
png_file_path = os.path.join(png_folder_path, '%s.png' % mri_file)
try:
# Convert the actual file
convert_file(mri_file_path, png_file_path)
print 'SUCCESS>', mri_file_path, '-->', png_file_path
except Exception as e:
print 'FAIL>', mri_file_path, '-->', png_file_path, ':', e
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Convert a dicom MRI file to png")
parser.add_argument('-f', action='store_true')
parser.add_argument('dicom_path', help='Full path to the mri file')
parser.add_argument('png_path', help='Full path to the generated png file')
args = parser.parse_args()
print args
if args.f:
convert_folder(args.dicom_path, args.png_path)
else:
convert_file(args.dicom_path, args.png_path)