import nibabel as nib import numpy as np im = nib.load(im_path) new_im = np.expand_dims(im.get_fdata().astype(np.float32),-1) nib.save(nib.Nifti1Image(new_im, affine=im.affine), im_path)