[Numpy-discussion] create a numpy array of images

Christopher Barker Chris.Barker at noaa.gov
Fri Jan 28 13:57:43 EST 2011


On 1/28/11 7:01 AM, Asmi Shah wrote:
> I am using python for a while now and I have a requirement of creating a
> numpy array of microscopic tiff images ( this data is 3d, meaning there are
> 100 z slices of 512 X 512 pixels.) How can I create an array of images?

It's quite straightforward to create a 3-d array to hold this kind of data:

image_block = np.empty((100, 512, 512), dtype=??)

now you can load it up by using some lib (PIL, or ???) to load the tif 
images, and then:

for i in images:
     image_block[i,:,:] = i


note that I put dtype to ??? up there. What dtype you want is dependent 
on what's in the tiff images -- tiff can hold just about anything. So if 
they are say, 16 bit greyscale, you'd want:

dtype=np.uint16

if they are 24 bit rgb, you might want a custom dtype (I don't think 
there is a 24 bit dtype built in):

RGB_type = np.dtype([('r',np.uint8),('g',np.uint8),('b',np.uint8)])

for 32 bit rgba, you can use the same approach, or just a 32 bit integer.

The cool thing is that you can make views of this array with different 
dtypes, depending on what's easiest for the given use case. You can even 
break out the rgb parts into different axis:

image_block = np.empty((100, 512, 512), dtype=RGB_type)

image_block_rgb=image_block.view(dtype=np.uint8).reshape((100,512,512,3))

The two arrays now share the same data block, but you can look at them 
differently.

I think this a really cool feature of numpy.

 >  i then would like to use visvis for visualizing this in 3D.

you'll have to see what visvis is expecting in terms of data types, etc.

HTH,

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list