[Neuroimaging] nibabel: probelm reading a nii.gz

Matthew Brett matthew.brett at gmail.com
Mon Jul 24 14:19:23 EDT 2017


Hi,

On Mon, Jul 24, 2017 at 6:39 PM, Reynolds, Richard C. (NIH/NIMH) [E]
<reynoldr at mail.nih.gov> wrote:
> Hi Gabriel,
>
>
> It is simple to modify with the nifti_tool program (from
>
> the nifticlib library), but I don't know whether you have
>
> that, e.g.
>
>
>    nifti_tool -mod_hdr -mod_field magic 'n+1' \
>
>                   -infiles C1_Rel_thres40.0.nii -prefix NEW.nii

Thanks - that's one easy way to do the fix.

If you wanted to use nibabel, you could also do the following:

"""
import nibabel as nib

fname = 'C1_Rel_thres40.0.nii.gz'

# The following just selects the right object to open the file - in
# this case zipfile.ZipFile
with nib.openers.Opener(fname) as fobj:
    hdr = nib.Nifti1Header.from_fileobj(fobj, check=False)  # don't raise error
    data = hdr.data_from_fileobj(fobj)
hdr['magic'] = b'n+1\x00'
img = nib.Nifti1Image(data, None, hdr)
nib.save(img, 'fixed.nii.gz')
"""

Cheers,

Matthew


More information about the Neuroimaging mailing list