I am looking to convert ~1000 .nrrd
files into Nifit (.nii.gz
) format. I've been using 3DSlicer's ResampleScalarVectorDWIVolume
command-line module to accomplish this task. But this process is really slow. It takes ~4 minutes to convert each file on my system.
I was wondering what tool do people use for such conversions?
The following code can be used for converting all the '.nrrd' files in a folder into compressed 'nifti' format:
import os
from glob import glob
import nrrd #pip install pynrrd, if pynrrd is not already installed
import nibabel as nib #pip install nibabel, if nibabel is not already installed
import numpy as np
baseDir = os.path.normpath('path/to/file')
files = glob(baseDir+'/*.nrrd')
for file in files:
#load nrrd
_nrrd = nrrd.read(file)
data = _nrrd[0]
header = _nrrd[1]
#save nifti
img = nib.Nifti1Image(data, np.eye(4))
nib.save(img,os.path.join(baseDir, file[-8:-5] + '.nii.gz'))
For example, this script would convert abc.nrrd
and xyz.nrrd
files in the baseDir
to abc.nii.gz
and xyz.nii.gz
respectively.