Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nifti file header and image
# https://bic-berkeley.github.io/psych-214-fall-2016/saving_images.html
def array_to_nii(array_3d, scan, output_path):
'''
Same affine and same header
convert mask only contains 0 and 1, so convert data type to uint8
:param array_3d:
:param scan:
:param output_path:
:return:
'''
data_type = np.uint8
array_3d = array_3d.astype(data_type)
nii_img = nib.Nifti1Image(array_3d, affine=scan.affine, header=scan.header)
nii_img.set_data_dtype(dtype=data_type)
nib.save(nii_img, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment