diff --git a/fsl/data/image.py b/fsl/data/image.py index 7eaf53f4f775333a097cf4eeab3249776141e197..39f1f7facdd789e197340177f8e73dea77507c31 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -864,22 +864,26 @@ class Nifti(notifier.Notifier, meta.Meta): raise ValueError('Exactly one of pixdim or ' 'shape must be specified') + if shape is not None: ndim = len(shape) + else: ndim = len(pixdim) + + # We only allow adjustment of + # the spatial dimensions + if ndim != 3: + raise ValueError('Three dimensions must be specified') + + oldShape = np.array(self.shape[ :ndim]) + oldPixdim = np.array(self.pixdim[:ndim]) newShape = shape newPixdim = pixdim # if pixdims were specified, - # convert them into a shape + # convert them into a shape, + # and vice versa if newPixdim is not None: - npixdim = len(newPixdim) - newPixdim = np.array(newPixdim) - oldShape = np.array(self.shape[ :npixdim]) - oldPixdim = np.array(self.pixdim[:npixdim]) - newShape = oldShape * (oldPixdim / newPixdim) - - # We only allow adjustment of - # the spatial dimensions - if len(newShape) != 3: - raise ValueError('Three dimensions must be specified') + newShape = oldShape * (oldPixdim / newPixdim) + else: + newPixdim = oldPixdim * (oldShape / newShape) # Rescale the voxel-to-world affine xform = affine.rescale(oldShape, newShape, origin) diff --git a/fsl/transform/affine.py b/fsl/transform/affine.py index 92a16ddd4382a4e60081d053961d21a97014ed8f..c7fb71f2a0571f8a1318a7e7b84749cfa498a9f2 100644 --- a/fsl/transform/affine.py +++ b/fsl/transform/affine.py @@ -588,7 +588,7 @@ def rmsdev(T1, T2, R=None, xc=None): def rescale(oldShape, newShape, origin=None): """Calculates an affine matrix to use for resampling. - This function generates an affine transformation matreix that can be used + This function generates an affine transformation matrix that can be used to resample an N-D array from ``oldShape`` to ``newShape`` using, for example, ``scipy.ndimage.affine_transform``.