@@ -107,26 +107,27 @@ def concat_images(images, check_affines=True, axis=None):
107
107
New image resulting from concatenating `images` across last
108
108
dimension
109
109
'''
110
+
110
111
n_imgs = len (images )
111
- img0 = images [0 ]
112
- if not hasattr (img0 , 'get_data' ):
113
- img0 = load (img0 )
114
- affine = img0 .affine
115
- header = img0 .header
116
- i0shape = img0 .shape
117
- del img0
118
-
119
- if axis is None : # collect images in output array for efficiency
120
- out_shape = (n_imgs , ) + i0shape [:3 ]
121
- out_data = np .empty (out_shape )
122
- else : # collect images in list for use with np.concatenate
123
- out_data = [None ] * n_imgs
112
+ if n_imgs == 0 :
113
+ raise ValueError ('Cannot concatenate an empty list of images.' )
124
114
125
115
for i , img in enumerate (images ):
126
116
if not hasattr (img , 'get_data' ):
127
117
img = load (img )
128
118
129
- if check_affines and not np .all (img .affine == affine ):
119
+ if i == 0 : # first image, initialize data from loaded image
120
+ affine = img .affine
121
+ header = img .header
122
+ klass = img .__class__
123
+
124
+ if axis is None : # collect images in output array for efficiency
125
+ out_shape = (n_imgs , ) + img .shape [:3 ]
126
+ out_data = np .empty (out_shape )
127
+ else : # collect images in list for use with np.concatenate
128
+ out_data = [None ] * n_imgs
129
+
130
+ elif check_affines and not np .all (img .affine == affine ):
130
131
raise ValueError ('Affines do not match' )
131
132
132
133
# Special case for 4D image with size[3] == 1; reshape to work!
@@ -148,7 +149,6 @@ def concat_images(images, check_affines=True, axis=None):
148
149
for di , data in enumerate (out_data )]
149
150
out_data = np .concatenate (out_data , axis = axis )
150
151
151
- klass = img0 .__class__
152
152
return klass (out_data , affine , header )
153
153
154
154
0 commit comments