2011/1/28 Christopher Barker <chris.bar...@noaa.gov>:
> 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

Notice that since PIL 1.1.6, PIL Image objects support the numpy
interface: http://effbot.org/zone/pil-changes-116.htm

>>> import PIL.Image
>>> im = PIL.Image.open('P1010102.JPG')
>>> im
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3264x2448 at 0x4CA0A8>
>>> a = numpy.asarray(im)
>>> a.shape
(2448, 3264, 3)
>>> a.dtype
dtype('uint8')

You can use the image just as any other ndarray:

>>> stack = numpy.empty((5, 2488, 3264, 3))
>>> stack[0] = im
and so on

for 5 images in a stack, notice that the dtype of the initially empty
ndarray is float!

It works also vice-versa:

>>> im_copy = PIL.Image.fromarray(a)

but this seems to require integer-valued ndarrays as input, except
when the ndarray is monochrome.

This might be even simpler than the dtype proposed by Christopher.

For more info on PIL: http://www.pythonware.com/library/pil/handbook/

Friedrich
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to