1

I have a tiff image of shape (6,500,500) and I read it as numpy array

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

I transpose it to make (500,500,6)

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

Then when I try to transform it into PIL image

>>> 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
>>

I get the above error.

My array is 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)
hi15
  • 2,113
  • 6
  • 28
  • 51

1 Answers1

1

From the source code of PIL (of the fromarray function https://github.com/python-pillow/Pillow/blob/master/PIL/Image.py) we can see that it can handle only 1, 2, 3 and 4 channel images (see the _fromarray_typemap dictionary in the above source code link). I think you should drop the non colour channels to be able to apply the fromarray function:

image = image[:,:,:3]
Srinivas V
  • 93
  • 7