Fixing NIFTI image files

Hi there,

I have some NIFTI images (being converted from DICOM/Philips) with wrong scale intercept values I would like to modify. Ideally I would like to fix them by modifying the scaling attributes in the NIFTI header while preserving the data as is now.

I have tried using nibabel for that but -to my limited knowledge about it- when loading a NIFTI image, nibabel is automatically applying the scale slope/intercept to the data values and setting the header values to NaN.

For now I found my way using FSL tools to do that but I was wondering if anyone knows an elegant way with nibabel to get all data as is stored in the file (plus the header) without automatically applying the scale parameters to it.

Thanks,
Jordi

Hi, @jhuguetn. First, I think you’ll get better help for a question like this on the nibabel issues page.

You can get the data object through img.dataobj, which will be an ArrayProxy which keeps track of the underlying array and the scale/intercept, in order to produce the data you get with img.get_data().

Assuming what you care about is the values, not the specific slope/intercept that gets stored in the header, what you could do is:

import nibabel as nib
img = nib.load(orig_fname)
scaled_data = new_inter + new_slope * img.dataobj.get_unscaled()
new_img = nib.Nifti1Image(scaled_data, img.affine, img.header)
new_img.to_filename(new_fname)

If you want to specifically replace the values, you may be able to set them in the new_img header. If this doesn’t get you all the way to where you’re trying to go, I’d again recommend moving over to Github.

Hi @effigies and thanks for your advice. My apologies if that’s not the appropriate forum for such a post.

Yes, that’s precisely what I was looking for actually:

Thanks for the aid!
Jordi

This is an appropriate forum; I was only suggesting you might get more informed help on GitHub.