将TIFF图像加载为numpy数组
问题描述:
我要在Python中加载一系列tiff图像.
I have a series of tiff images to load in Python.
首先,我使用:
im=Image.open(*)
它加载并正确显示.
>>> im
PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1408x1044 at 0x116154050
>>> type(im)
instance
>>> im.size
(1408, 1044)
然后我使用:
imarray=numpy.array(im)
其中
>>> imarray.shape
()
>>> imarray.size
1
>>> type(imarray)
numpy.ndarray
>>> imarray
array(PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1408x1044 at 0x116154050, dtype=object)
我已阅读上一篇文章和按照那里的说明进行操作,但是我无法获得imarray.shape
和im.size
匹配.
I have read this previous post and followed the instructions there, but I can't get imarray.shape
and im.size
to match.
答
对于TIFF图像,您只需使用imageio
For TIFF image, you can simply use imageio
im = imageio.imread('filename')
有时您可能进一步需要
im = np.array(im)