Skip to content

Commit fea5e80

Browse files
committed
More Python3 support + read images without .dcm extension
1 parent 58a8d6b commit fea5e80

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2015 Jan Pipek
3+
Copyright (c) 2014-2016 Jan Pipek
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

pydiq/dicom_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def array(self):
5555
def get_slice(self, plane, n):
5656
if plane not in ALLOWED_PLANES:
5757
raise ValueError("Invalid plane identificator (allowed are 0,1,2)")
58-
index = [slice(None, None, None) for i in xrange(3)]
58+
index = [slice(None, None, None) for i in range(3)]
5959
index[plane] = n
6060
return self._array[index]
6161

pydiq/utils.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import dicom
22
from collections import OrderedDict
3+
import os
34

45

56
def get_id(path):
67
f = dicom.read_file(path, stop_before_pixels=True)
78
return f.StudyInstanceUID, f.SeriesInstanceUID
89

9-
# def
10+
11+
def is_dicom_file(path):
12+
"""Fast way to check whether file is DICOM."""
13+
if not os.path.isfile(path):
14+
return False
15+
try:
16+
with open(path, "rb") as f:
17+
return f.read(132).decode("ASCII")[-4:] == "DICM"
18+
except:
19+
return False
20+
21+
def dicom_files_in_dir(directory="."):
22+
"""Full paths of all DICOM files in the directory."""
23+
directory = os.path.expanduser(directory)
24+
candidates = [os.path.join(directory, f) for f in sorted(os.listdir(directory))]
25+
return [f for f in candidates if is_dicom_file(f)]

pydiq/viewer.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .dicom_data import DicomData
1111
from .dicom_widget import DicomWidget
12+
from .utils import dicom_files_in_dir
1213

1314

1415
class TrackingLabel(QtGui.QLabel):
@@ -115,9 +116,9 @@ def __init__(self, path = None):
115116
self.update_cw()
116117

117118
if os.path.isfile(path):
118-
self.load_files(path)
119+
self.load_files([path])
119120
elif os.path.isdir(path):
120-
self.load_files(os.path.join(path, "*.dcm"))
121+
self.load_files(dicom_files_in_dir(path))
121122
self.build_menu()
122123

123124
def open_directory(self):
@@ -126,7 +127,8 @@ def open_directory(self):
126127
dialog.setViewMode(QtGui.QFileDialog.List)
127128
dialog.setOption(QtGui.QFileDialog.ShowDirsOnly, True)
128129
if dialog.exec_():
129-
self.load_files(os.path.join(str(dialog.selectedFiles()[0]), "*.dcm"))
130+
directory = str(dialog.selectedFiles()[0])
131+
self.load_files(dicom_files_in_dir(directory))
130132

131133
def export_image(self):
132134
file_name = QtGui.QFileDialog.getSaveFileName(
@@ -180,12 +182,9 @@ def on_file_item_change(self):
180182
# print item.text()
181183
self.file_name = str(item.toolTip())
182184

183-
def load_files(self, glob_string):
185+
def load_files(self, files):
184186
self.file_list.clear()
185-
if glob_string:
186-
self.files = sorted(glob.glob(glob_string))
187-
else:
188-
self.files = ()
187+
self.files = files
189188
for file_name in self.files:
190189
item = QtGui.QListWidgetItem(os.path.basename(file_name))
191190
item.setToolTip(file_name)

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy
2+
pydicom

0 commit comments

Comments
 (0)