File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,10 @@ def save(img, filename):
105
105
# Inline imports, as this module really shouldn't reference any image type
106
106
from .nifti1 import Nifti1Image , Nifti1Pair
107
107
from .nifti2 import Nifti2Image , Nifti2Pair
108
+
109
+ klass = None
110
+ converted = None
111
+
108
112
if type (img ) == Nifti1Image and lext in ('.img' , '.hdr' ):
109
113
klass = Nifti1Pair
110
114
elif type (img ) == Nifti2Image and lext in ('.img' , '.hdr' ):
@@ -119,8 +123,23 @@ def save(img, filename):
119
123
if not valid_klasses : # if list is empty
120
124
raise ImageFileError ('Cannot work out file type of "%s"' %
121
125
filename )
122
- klass = valid_klasses [0 ]
123
- converted = klass .from_image (img )
126
+
127
+ # Got a list of valid extensions, but that's no guarantee
128
+ # the file conversion will work. So, try each image
129
+ # in order...
130
+ for klass in valid_klasses :
131
+ try :
132
+ converted = klass .from_image (img )
133
+ break
134
+ except Exception as e :
135
+ continue
136
+ # ... and if none of them work, raise an error.
137
+ if converted is None :
138
+ raise e
139
+
140
+ # Here, we either have a klass or a converted image.
141
+ if converted is None :
142
+ converted = klass .from_image (img )
124
143
converted .to_filename (filename )
125
144
126
145
You can’t perform that action at this time.
0 commit comments