Load a tiff stack in a numpy array with python

后端 未结 2 1815
一个人的身影
一个人的身影 2021-02-05 19:59

I am having a little issue with .tif files. I am sure it is only a minor problem that I can´t get around (keep in mind, I am a relatively new programmer).

Basically: I h

2条回答
  •  渐次进展
    2021-02-05 20:25

    I am not sure if there is a way to get PIL to open multiple slices of a tiff stack.

    If you are not bound to using PIL, however, an alternative is scikit-image, which opens multiple slices from a tiff stack by default. Here is some sample code of how to load a tiff stack into a Numpy array using scikit-image:

    >>> from skimage import io
    >>> im = io.imread('an_image.tif')
    >>> print(im.shape)
    (2, 64, 64)
    

    Note that the imread function loads the image directly into a Numpy array. Also, the dimensions of the resulting array are ordered (z, y, x) where z represents the depth, y represents the height, and x represents the width. Thus, to get a single slice from the stack all you have to do is:

    >>> print(im[1].shape)
    (64, 64)
    

提交回复
热议问题