nipy / nibabel

Python package to access a cacophony of neuro-imaging file formats
http://nipy.org/nibabel/
Other
649 stars 258 forks source link

How to convert multiple Nifti1Image of different sizes to a nii.gz #1222

Closed emmanuel-nwogu closed 1 year ago

emmanuel-nwogu commented 1 year ago

I have an equally-sized DICOM series whose images I crop using Otsu thresholding and contouring. This cropping leads to smaller images with various sizes (width, height). These cropped images are represented as 2D grayscale numpy arrays in my code. Currently, I am able to convert each image to a Nifti1Image using code resembling the piece below.

# Inspired by https://stackoverflow.com/a/28331076
import nibabel as nib
import numpy as np

data = np.arange(4*4).reshape(4,4) # This is the cropped image data
new_image = nib.Nifti1Image(data, affine=np.eye(4))

I am curious if there's a way to write these Nifti1Image objects, all containing differently sized image data, to a single "nii.gz" file.

EDIT: I would like to preserve the image sizes of these Nifti1Image objects too.