working with raw image files

Wanderer wanderer at dialup4less.com
Mon Jun 13 16:20:30 EDT 2011


On Jun 13, 4:08 pm, Wanderer <wande... at dialup4less.com> wrote:
> On Jun 13, 2:18 pm, kafooster <dmoze... at gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > I am working on some medical image data, and I try to look into
> > specific slice of   3d  *.raw image. I know voxels are 16 bit int, and
> > dimensions are 352*470*96. I checked it in some pro medical image
> > viewer, it is alright. However, with the code I use, I display just
> > white noise image.(but worked well for other, 8bit raw image).
> >  Also, printed size is half the original size, like it was 8 bit. I
> > read some documentations on PIL, numpy etc but I think I just do not
> > understand something.
> > I open image data set, I change it to array, give it dimensions,
> > dtype, and change it to image, right? I think there is something
> > messed up in 'binvalues', but dont really know how to write it in
> > simpler way.
>
> > P.S.1
> > If I want to change data type to e.g. 8 bit uint, is it just change in
> > scipy.array? or it requires some more changes
>
> > P.S.2
> > Lets say I have my array of image data and want to save it to *.raw
> > data set. is it array.tofile?
>
> > Here is my code
>
> > ############################
>
> > import scipy as sc
> > from pylab import *
> > import array
> > import Image
>
> > fileobj = open("hand.raw", 'rb')
> > binvalues = array.array('B')
> > binvalues.read (fileobj, 352*470*96)
> > data1 = sc.array(binvalues, dtype=sc.int16)
> > data2 = sc.reshape(data1, (352,470,96))
> > fileobj.close()
> > print data2.size , data2.dtype
>
> > im = Image.fromarray(data2[:,:,40])
> > im.show()
>
> Try using numpy arrays.
>
> import numpy as np
> import Image
>
> image1 = Image.open("hand.raw", 'rb')
> imshape = image1.size
> npArray = np.array(image1.getdata())
> npArray.shape = imshape
>
> im = Image.fromarray(npArray)
> im.show()

P.S.1
If you want to change data size from a data buffer, you could use
something like.

 image1 = np.frombuffer(Buffer, np.uint16)

P.S.2
I'm not sure what a *.raw file is but if Image has support for it you
just need to include the extension.

 im = Image.fromarray(npArray)
 im.save(self.resultDir + "\\" + imageName + '.tif')






More information about the Python-list mailing list