[Neuroimaging] writing a 1D python array to 3D Nifti file

Christopher Markiewicz markiewicz at stanford.edu
Mon May 9 10:05:43 EDT 2022

Hi Lorenzo,

Assuming all you did was flatten the array, you should be able to simply reshape the array to the image shape:

    new_img = Nifti1Image(arr.reshape(old_img.shape), old_img.affine, old_img.header)​

Note that this preserves the affine and type of the input image, so if either of those has changed under your transformations, then you will need to pass the updated affine and call set_data_dtype()​ after creation.

From: Neuroimaging <neuroimaging-bounces+markiewicz=stanford.edu at python.org> on behalf of Lorenzo Pini <pini.lorenzo2 at gmail.com>
Sent: Monday, May 9, 2022 09:59
To: Neuroimaging analysis in Python <neuroimaging at python.org>
Subject: [Neuroimaging] writing a 1D python array to 3D Nifti file

Dear experts,

I uploaded a 3D nifti file with nibabel, transformed it into a 1D array and performed some transformations. My final 1D array has size 902629 (MNI vector space).

Now, I would like to convert this 1D array back to a 3D nifti image. Any advice to perform this operation?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/neuroimaging/attachments/20220509/acf63ecc/attachment.html>

More information about the Neuroimaging mailing list