[Neuroimaging] Change sform and save

Matthew Brett matthew.brett at gmail.com
Mon Aug 3 14:30:30 CEST 2015


Hi,

On Mon, Aug 3, 2015 at 1:19 PM, Markus Gramer <Markus.Gramer at kuleuven.be> wrote:
> Hi there,
>
> I am new to nibabel (and also python) so maybe I am making an obvious
> mistake, but I could not find anything in the manual that would help me…
>
> I am trying to update the sform in a Nifty1 image and save it with:
>
> def update_sform(file_in, file_out, affine_out):
>
> img = nib.load(file_in)
>
> img_hdr = img.header
>
> img_hdr.set_sform(affine_out)
>
> nib.save(img,file_out)
>
>
> However, I realised that due to the comparison between current header and
> affine:
>
>
> np.allclose(self._affine, hdr.get_best_affine()) [spatialimages.py, line
> 426]
>
>
> The sform in the header is reset to the affine when I am saving the image.
> Is there a way to directly set the affine? Or any other method that I can
> use to achieve this?

Sorry - it should be better documented, but I think you want:

img.set_sform(affine_out)

This sets the affine for the image as well as the sform,

Cheers,

Matthew


More information about the Neuroimaging mailing list