[Neuroimaging] Nibabel -how to convert nii to jpg?

Matthew Brett matthew.brett at gmail.com
Fri Jul 8 05:49:44 EDT 2022


Hi,

‪On Fri, Jul 8, 2022 at 10:16 AM ‫נתי שטרן‬‎ <nsh531 at gmail.com> wrote:‬
>
> I glad for any help
> _______________________________________________

As a pointer, you'll need to load the image:

import nibabel as nib
img = nib.load('my_image.nii')
data = img.get_fdata()

and then take some 2D slice (assuming, below, that you have a 3D image above):

my_slice = data[..., 15]

And finally you need to write it out using imagio (pip install imageio):

import imageio
imageio.imwrite('my_slice.jpg', my_slice)

You'll get a warning at that point, about conversion to uint8 - you
can either do that conversion yourself, by rescaling the floating
point slice to uint8, or try another output format.

Cheers,

Matthew


More information about the Neuroimaging mailing list