Skip to content

Instantly share code, notes, and snippets.

@mingrui
Created October 23, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingrui/550a7657444e39ffd408ec8b5935c753 to your computer and use it in GitHub Desktop.
Save mingrui/550a7657444e39ffd408ec8b5935c753 to your computer and use it in GitHub Desktop.
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