PIL Image.fromarray抱怨6通道tiff数据

时间:2017-11-07 05:37:09

标签: python numpy python-imaging-library uint8t

我有一个形状(6,500,500)的tiff图像,我把它读成numpy数组

>>>type(image)
    <type 'numpy.ndarray'>
>>> image.shape
(6, 500, 500)

我将其转置为制作(500,500,6)

image = image.transpose((1, 2, 0))
>>> image.shape
(500, 500, 6)

然后当我尝试将其转换为PIL图像时

>>> image = Image.fromarray(image)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dfd/env27/local/lib/python2.7/site-packages/PIL/Image.py", line 2419, in fromarray
    raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type
>>

我收到了上述错误。

我的数组是uint8

image
array([[[122, 104,  77, 255, 145,  71],
        [123, 102,  77, 252, 140,  71],
        [122,  99,  72, 249, 123,  57],


       [[133, 113,  90, 106,  45,  22],
        [129,  98,  77,  96,  36,  18],
        [126,  99,  77, 102,  39,  18],
        ..., 
        [124, 105,  76, 255, 120,  54],
        [123, 104,  74, 254, 114,  51],
        [119, 102,  69,   8, 117,  51]]], dtype=uint8)

1 个答案:

答案 0 :(得分:0)

从PIL的源代码(fromarray函数https://github.com/python-pillow/Pillow/blob/master/PIL/Image.py)我们可以看到它只能处理1,2,3和4个通道图像(参见上面源代码链接中的_fromarray_typemap字典)。我认为你应该删除非颜色通道才能应用fromarray功能:

image = image[:,:,:3]