问题应该解决了,就是把源文件的矩阵用来转Nifti1Image生成的文件(讲得可能不太清楚,大概是这样)。code如下:
import nibabel as nib import numpy as np data = np.asarray(nib.load(‘import_path’).get_fdata()) print(data) arr = data.astype(‘float64’) arr[arr == 0] = ‘nan’ # or use np.nan print(arr) newimage = nib.Nifti1Image(arr, np.eye(4)) img = nib.load(‘import_path’) img_affine = img.affine new_image = new_image.get_data() nib.Nifti1Image(new_image,img_affine).tofilename(‘save_path’)