-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.py
343 lines (255 loc) · 13.4 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from tkinter import *
from tkinter import filedialog
from tkinter.ttk import Progressbar
from functions import convert_image_to_dicom, convert_dcm_jpg, convert_nifti_to_dicom
from glob import glob
import dicom2nifti
import os
def progress_bar(master):
progress_bar = Progressbar(master, mode='determinate', orient='horizontal', length=500)
progress_bar.pack(pady=(20,0))
return progress_bar
def call_home_page():
root.geometry('700x500')
home_page = Frame(root, bg=bg)
home_page.grid(row=0, column=0, sticky='nsew')
title = Label(home_page, text='Medical Conversions', bg=bg, fg='#ffffff', font='Arial 35 bold')
title.pack(pady=(20,0))
buttons_frame = Frame(home_page, bg=bg)
buttons_frame.pack(pady=(50,0))
image_to_dicom_button = Button(buttons_frame, text='Image\nto\nDicom', font='none 20 bold', width=10, fg='#053047', command=image_to_dicom_page)
image_to_dicom_button.grid(row=0, column=0)
dicom_to_image_button = Button(buttons_frame, text='Dicom\nto\nImage', font='none 20 bold', width=10, fg='#053047', command=dicom_to_image_page)
dicom_to_image_button.grid(row=0, column=1, padx=(50,0))
dicom_to_nifti_button = Button(buttons_frame, text='Dicom\nto\nNifti', font='none 20 bold', width=10, fg='#053047', command=dicom_to_nifti_page)
dicom_to_nifti_button.grid(row=1, column=0, pady=(50,0))
nifti_to_dicom_button = Button(buttons_frame, text='Nifti\nto\nDicom', font='none 20 bold', width=10, fg='#053047', command=nifti_to_dicom_page)
nifti_to_dicom_button.grid(row=1, column=1, padx=(50,0), pady=(50,0))
def image_to_dicom_page():
global text_message_i_d
root.geometry('600x450')
image_to_dicom = Frame(root, bg=bg)
image_to_dicom.grid(row=0, column=0, sticky='nsew')
title = Label(image_to_dicom, text='Image to Dicom', bg=bg, fg='#ffffff', font='Arial 35 bold')
title.pack()
open_buttons = Frame(image_to_dicom, bg=bg)
open_buttons.pack(pady=(30,0))
open_file = Button(open_buttons, text='Open File', font='none 20 bold', width=10, fg='#053047', command=call_open_file_image_to_dicom)
open_file.grid(row=0, column=0, padx=(0,20))
open_dir = Button(open_buttons, text='Open Dir', font='none 20 bold', width=10, fg='#053047', command=call_open_dir_image_to_dicom)
open_dir.grid(row=0, column=1, padx=(20,0))
convert_save = Button(image_to_dicom, text='Convert & Save', font='none 20 bold', fg='#053047', command=call_convert_save_image_to_dicom)
convert_save.pack(pady=(40,0))
text_message_i_d = Label(image_to_dicom,text='Choose file or dir', font='none 9', bg=bg, fg='#FFFFFF')
text_message_i_d.pack(pady=(20,0))
home_button = Button(image_to_dicom, text='Home', command=call_home_page, font='none 13 bold', width=10, fg='#053047')
home_button.pack(pady=(40,20))
def dicom_to_image_page():
global dicom_to_image
global text_message_d_i
root.geometry('600x450')
dicom_to_image = Frame(root, bg=bg)
dicom_to_image.grid(row=0, column=0, sticky='nsew')
title = Label(dicom_to_image, text='Dicom to Image', bg=bg, fg='#ffffff', font='Arial 35 bold')
title.pack()
open_buttons = Frame(dicom_to_image, bg=bg)
open_buttons.pack(pady=(30,0))
open_file = Button(open_buttons, text='Open File', font='none 20 bold', width=10, fg='#053047', command=call_open_file_dicom_to_image)
open_file.grid(row=0, column=0, padx=(0,20))
open_dir = Button(open_buttons, text='Open Dir', font='none 20 bold', width=10, fg='#053047', command=call_open_dir_dicom_to_image)
open_dir.grid(row=0, column=1, padx=(20,0))
convert_save = Button(dicom_to_image, text='Convert & Save', font='none 20 bold', fg='#053047', command=call_convert_save_dicom_to_image)
convert_save.pack(pady=(40,0))
text_message_d_i = Label(dicom_to_image,text='Choose file or dir', font='none 9', bg=bg, fg='#FFFFFF')
text_message_d_i.pack(pady=(20,0))
home_button = Button(dicom_to_image, text='Home', command=call_home_page, font='none 13 bold', width=10, fg='#053047')
home_button.pack(pady=(40,20))
def dicom_to_nifti_page():
global text_message_d_n
root.geometry('600x450')
dicom_to_nifti = Frame(root, bg=bg)
dicom_to_nifti.grid(row=0, column=0, sticky='nsew')
title = Label(dicom_to_nifti, text='Dicom to Nifti', bg=bg, fg='#ffffff', font='Arial 35 bold')
title.pack()
open_buttons = Frame(dicom_to_nifti, bg=bg)
open_buttons.pack(pady=(30,0))
open_file = Button(open_buttons, text='Open Dir', font='none 20 bold', width=10, fg='#053047', command=call_open_file_dicom_to_nifti)
open_file.grid(row=0, column=0, padx=(0,20))
open_dir = Button(open_buttons, text='Open Dirs', font='none 20 bold', width=10, fg='#053047', command=call_open_dir_dicom_to_nifti)
open_dir.grid(row=0, column=1, padx=(20,0))
convert_save = Button(dicom_to_nifti, text='Convert & Save', font='none 20 bold', fg='#053047', command=call_convert_save_dicom_to_nifti)
convert_save.pack(pady=(40,0))
text_message_d_n = Label(dicom_to_nifti,text='Choose file or dir', font='none 9', bg=bg, fg='#FFFFFF')
text_message_d_n.pack(pady=(20,0))
home_button = Button(dicom_to_nifti, text='Home', command=call_home_page, font='none 13 bold', width=10, fg='#053047')
home_button.pack(pady=(40,20))
def nifti_to_dicom_page():
global text_message_n_d
root.geometry('600x450')
nifti_to_dicom = Frame(root, bg=bg)
nifti_to_dicom.grid(row=0, column=0, sticky='nsew')
title = Label(nifti_to_dicom, text='Nifti to Dicom', bg=bg, fg='#ffffff', font='Arial 35 bold')
title.pack()
open_buttons = Frame(nifti_to_dicom, bg=bg)
open_buttons.pack(pady=(30,0))
open_file = Button(open_buttons, text='Open File', font='none 20 bold', width=10, fg='#053047', command=call_open_file_nifti_dicom)
open_file.grid(row=0, column=0, padx=(0,20))
open_dir = Button(open_buttons, text='Open Dir', font='none 20 bold', width=10, fg='#053047', command=call_open_dir_nifti_to_dicom)
open_dir.grid(row=0, column=1, padx=(20,0))
convert_save = Button(nifti_to_dicom, text='Convert & Save', font='none 20 bold', fg='#053047', command=call_convert_save_nifti_to_dicom)
convert_save.pack(pady=(40,0))
text_message_n_d = Label(nifti_to_dicom,text='Choose file or dir', font='none 9', bg=bg, fg='#FFFFFF')
text_message_n_d.pack(pady=(20,0))
home_button = Button(nifti_to_dicom, text='Home', command=call_home_page, font='none 13 bold', width=10, fg='#053047')
home_button.pack(pady=(40,20))
def call_open_file_image_to_dicom():
global flag_image_dicom
global in_path_image_dicom
global text_message_i_d
in_path_image_dicom = filedialog.askopenfile()
if in_path_image_dicom:
flag_image_dicom = 1
text_message_i_d.config(text='You opened: \n' + in_path_image_dicom.name)
def call_open_dir_image_to_dicom():
global flag_image_dicom
global in_path_image_dicom
global text_message_i_d
in_path_image_dicom = filedialog.askdirectory()
if in_path_image_dicom:
flag_image_dicom = 2
text_message_i_d.config(text='You opened: \n' + in_path_image_dicom)
def call_convert_save_image_to_dicom():
global text_message_i_d
if flag_image_dicom == 1 and in_path_image_dicom:
out_path = filedialog.asksaveasfilename()
if out_path:
convert_image_to_dicom(in_path_image_dicom.name, out_path)
text_message_i_d.config(text='File saved at\n' + out_path + '.dcm')
if flag_image_dicom == 2 and in_path_image_dicom:
images = glob(in_path_image_dicom + '/*')
out_path = filedialog.askdirectory()
if out_path:
for i, image in enumerate(images):
convert_image_to_dicom(image, out_path + '/file' + str(i).zfill(len(str(len(images)))))
text_message_i_d.config(text='Files saved at\n' + out_path)
def call_open_file_dicom_to_image():
global flag_dicom_image
global in_path_dicom_image
global text_message_d_i
in_path_dicom_image = filedialog.askopenfile()
if in_path_dicom_image:
flag_dicom_image = 1
text_message_d_i.config(text='You opened: \n' + in_path_dicom_image.name)
def call_open_dir_dicom_to_image():
global flag_dicom_image
global in_path_dicom_image
global text_message_d_i
in_path_dicom_image = filedialog.askdirectory()
if in_path_dicom_image:
flag_dicom_image = 2
text_message_d_i.config(text='You opened: \n' + in_path_dicom_image)
def call_convert_save_dicom_to_image():
global text_message_d_i
if flag_dicom_image == 1 and in_path_dicom_image:
out_path = filedialog.asksaveasfilename()
if out_path:
convert_dcm_jpg(in_path_dicom_image.name, out_dir=out_path)
text_message_d_i.config(text='File saved at\n' + out_path + '.dcm')
if flag_dicom_image == 2 and in_path_dicom_image:
images = glob(in_path_dicom_image + '/*.dcm')
out_path = filedialog.askdirectory()
if out_path:
for i, image in enumerate(images):
convert_dcm_jpg(image, out_path + '/image' + str(i).zfill(len(str(len(images)))))
text_message_d_i.config(text='Files saved at\n' + out_path)
def call_open_file_dicom_to_nifti():
global flag_dicom_nifti
global in_path_dicom_nifti
global text_message_d_n
in_path_dicom_nifti = filedialog.askdirectory()
if in_path_dicom_nifti:
flag_dicom_nifti = 1
text_message_d_n.config(text='You opened: \n' + in_path_dicom_nifti)
def call_open_dir_dicom_to_nifti():
global flag_dicom_nifti
global in_path_dicom_nifti
global text_message_d_n
in_path_dicom_nifti = filedialog.askdirectory()
if in_path_dicom_nifti:
flag_dicom_nifti = 2
text_message_d_n.config(text='You opened: \n' + in_path_dicom_nifti)
def call_convert_save_dicom_to_nifti():
global text_message_d_n
text_message_d_n.config(text='Converting...')
if flag_dicom_nifti == 1 and in_path_dicom_nifti:
out_path = filedialog.asksaveasfilename()
if out_path:
dicom2nifti.dicom_series_to_nifti(in_path_dicom_nifti, out_path + '.nii.gz')
text_message_d_n.config(text='File saved at\n' + out_path + '.nii.gz')
if flag_dicom_nifti == 2 and in_path_dicom_nifti:
images = glob(in_path_dicom_nifti + '/*')
out_path = filedialog.askdirectory()
if out_path:
for i, image in enumerate(images):
text_message_d_n.config(text='Converting...')
dicom2nifti.dicom_series_to_nifti(image, out_path + '/' + os.path.basename(image) + str(i).zfill(len(str(len(images)))) + '.nii.gz')
text_message_d_n.config(text='Files saved at\n' + out_path)
def call_open_file_nifti_dicom():
global flag_nifti_dicom
global in_path_nifti_dicom
global text_message_n_d
in_path_nifti_dicom = filedialog.askopenfilename()
if in_path_nifti_dicom:
flag_nifti_dicom = 1
text_message_n_d.config(text='You opened: \n' + in_path_nifti_dicom)
def call_open_dir_nifti_to_dicom():
global flag_nifti_dicom
global in_path_nifti_dicom
global text_message_n_d
in_path_nifti_dicom = filedialog.askdirectory()
if in_path_nifti_dicom:
flag_nifti_dicom = 2
text_message_n_d.config(text='You opened: \n' + in_path_nifti_dicom)
def call_convert_save_nifti_to_dicom():
global text_message_n_d
text_message_n_d.config(text='Converting...')
if flag_nifti_dicom == 1 and in_path_nifti_dicom:
out_path = filedialog.askdirectory()
if out_path:
convert_nifti_to_dicom(in_path_nifti_dicom, out_path)
text_message_n_d.config(text='File saved at\n' + out_path + '.nii.gz')
if flag_nifti_dicom == 2 and in_path_nifti_dicom:
images = glob(in_path_nifti_dicom + '/*')
out_path = filedialog.askdirectory()
if out_path:
for i, image in enumerate(images):
text_message_n_d.config(text='Converting...')
o_path = out_path + '/' + os.path.basename(image)[:-7]
if not os.path.exists(o_path): os.makedirs(o_path)
convert_nifti_to_dicom(image, o_path)
text_message_n_d.config(text='Files saved at\n' + out_path)
##############################################################################
########################## This is the main function #########################
##############################################################################
if __name__ == '__main__':
global in_path_image_dicom
global in_path_dicom_image
global in_path_nifti_dicom
global in_path_dicom_nifti
global flag_image_dicom
global flag_dicom_image
global flag_nifti_dicom
global flag_dicom_nifti
flag_image_dicom = 0
flag_dicom_image = 0
flag_nifti_dicom = 0
flag_dicom_nifti = 0
bg = '#053047'
root = Tk()
root.geometry('700x500')
root.title('Pycad Convert')
# root.iconbitmap('utils/logo.ico')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
call_home_page()
root.mainloop()