[Neuroimaging] [Nibabel] save modified nifti header to file

Matthew Brett matthew.brett at gmail.com
Wed Dec 19 09:12:13 EST 2018


Hi,

On Wed, Dec 19, 2018 at 1:01 PM Daniel Münch
<daniel.muench at research.fchampalimaud.org> wrote:
>
> Hi,
>
> I am loading a nifti1 file using nib.load(), then modify some header
> fields. When I now save using nib.save() the whole image gets loaded to
> the memory and saved again (which takes forever).
>
> As I only modified the header is there a way to only write these
> modifications to the file?

You could try something like this (assuming the size of the header
isn't going to change):

[nav] In [18]: with open('test.nii', 'r+b') as fobj:
          ...:     header = nib.Nifti1Header.from_fileobj(fobj)
          ...:     header['descrip'] = b'modified'
          ...:     fobj.seek(0)
          ...:     header.write_to(fobj)

Cheers,

Matthew


More information about the Neuroimaging mailing list