[Neuroimaging] RGB Nifti

Fernando Pérez-García fepegar at gmail.com
Tue Feb 16 11:05:41 EST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20160216/213bd41d/attachment.html>


More information about the Neuroimaging mailing list