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

Christophe Pallier christophe at pallier.org
Fri Jul 8 06:26:20 EDT 2022


In case you can install it, the nilearn package, which relies on nibabel,
offers a variety of functions to display nifti (.nii) files.
see https://nilearn.github.io/stable/plotting/index.html

--
Christophe Pallier (http://www.pallier.org)
INSERM Cognitive Neuroimaging Lab (http://www.unicog.org)


On Fri, Jul 8, 2022 at 11:50 AM Matthew Brett <matthew.brett at gmail.com>
wrote:

> 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
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/neuroimaging/attachments/20220708/9927de52/attachment.html>


More information about the Neuroimaging mailing list