'How to convert numpy array to nifiti file?

It's as simple as that :) I hope this help you out as well.

import numpy as np
import nibabel as nib

converted_array = np.array(normal_array, dtype=np.float32) # You need to replace normal array by yours
affine = np.eye(4)
nifti_file = nib.Nifti1Image(converted_array, affine)

nib.save(nifti_file, path_to_save) # Here you put the path + the extionsion 'nii' or 'nii.gz'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source