Skip to content

Difference in saving nifti files in nibabel #825

@leonardo-oz

Description

@leonardo-oz

Dear Nibabel Experts:

I am following nipype tutorial to save files. One approach is to use nib.save(). The other is to use image.to_filename(). But files saved from two methods seems quite different with fsleyes. The latter one is consistent with input file, while the formal is not. Please check the picture below. I was expecting both a and b should perfectly match. I intend to use the formal one, as image_data = image_get_fdata() can give me a numpy array which is easy to get processed for other purpose. Can someone point out what causes the difference in saving and how to fix it? Thanks. The zipped mask file is attached.
mask.nii.gz

The code snippet is below:

    image = nib.load("mask.nii.gz")
    # save method 1
    image.to_filename("a.nii.gz")
    
    # save method 2
    outImg = nib.Nifti1Image(image.get_fdata(), affine=image.header.get_base_affine(), 
    header=image.header)
    nib.save(outImg, "b.nii.gz")

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions