Skip to content

Instantly share code, notes, and snippets.

@ofgulban
Last active January 18, 2021 19:00
Show Gist options
  • Save ofgulban/df2a0a2437a13055b84e09dd4a345c91 to your computer and use it in GitHub Desktop.
Save ofgulban/df2a0a2437a13055b84e09dd4a345c91 to your computer and use it in GitHub Desktop.
Make affine matrix of a nifti file identity matrix.
"Make affine matrix of a nifti file identity matrix."
import os
import nibabel as nb
import numpy as np
NII = "/path/to/tile.nii.gz"
nii = nb.load(NII)
data = np.asarray(nii.dataobj)
hdr = nii.header
hdr.set_sform(hdr.get_qform())
aff = hdr.get_qform()
img = nb.Nifti1Image(data, affine=aff, header=hdr)
basename, ext = nii.get_filename().split(os.extsep, 1)
out_name = '{}_{}.{}'.format(basename, "fixed", ext)
nb.save(img, out_name)
print("Finished.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment