[Neuroimaging] RGB Nifti

paulo rodrigues khamael at gmail.com
Tue Feb 16 11:32:31 EST 2016


Hi Fernando,

Did you have a look at ants? It has some tools for that kind of operations:
check ConvertScalarImageToRGB

Cheers,
Paulo

On Tue, Feb 16, 2016 at 5:05 PM, Fernando Pérez-García <fepegar at gmail.com>
wrote:

> Dear Nibabel experts,
>
> I'm trying to create an RGB nifti image from a PNG file. I've been messing
> around with a working RGB nifti, in order to mimic the datatype found in
> its header. So far I've managed to convert the image pixel-wise with for
> loops, which is very slow. Do you know how this could be done in a faster
> way?
>
>
> My code:
>
> import numpy as np
> import Image
> import nibabel as nib
>
> p = '/home/fernando/test/nii_rgb/TC1.png'
> im = Image.open(p)
>
> data = np.array(im)
> data = np.rot90(data)
> rgb = np.zeros((data.shape[0], data.shape[1], 1, 1), [('R', 'u1'), ('G',
> 'u1'), ('B', 'u1')])
>
> for i in range(data.shape[0]):
>     for j in range(data.shape[1]):
>         rgb[i, j] = tuple(data[i, j, :])
>
> nii = nib.Nifti1Image(rgb, np.eye(4))
> nib.save(nii, p.replace('png', 'nii'))
>
>
>
> Thanks in advance,
>
> Fernando
>
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20160216/e8e928ce/attachment.html>


More information about the Neuroimaging mailing list