2

I have a 4 channel Numpy image that needs to be converted to PIL image in order implement torchvision transformations on image. But when I try to do this using PIL.Image.from_array(<my_numpy_image>) I get the following error.

TypeError: Cannot handle this data type

I cannot loose and get rid of any channel information right now so won't be able to discard any channels.

thanatoz
  • 2,365
  • 4
  • 15
  • 39

1 Answers1

2

Try specifying mode so that PIL is aware of data format.

img = Image.fromarray(source_array, mode="CMYK")

If that does not work, what is the shape of source array ?

Shamit Verma
  • 2,239
  • 1
  • 8
  • 14
  • The shape of source arrays are (1400,2100) and they are all consistent in shape. What will happen if the channel number is even greater? – thanatoz Aug 21 '19 at 17:45
  • Why does this array have only two dimensions ? Source array should be 4D (#Images, #Channels, X_dim,Y_dim). # of channels can be 1 (Grayscale) 3 (RGB) or 4 (RGBA / CMYK) – Shamit Verma Aug 22 '19 at 02:50
  • I was just representing the shape of every individual channel with its 2d matrix. The shape of the complete array is (1400,2100,4). But it is not a CMYK image but just an arbitrary arrays of this shape. But I believe that we can treat it like a cmyk image. This should help. – thanatoz Aug 22 '19 at 03:03
  • But what if I increase one more channel in the image? How will I be able to handle that data too in the future? – thanatoz Aug 22 '19 at 03:04
  • (1400,2100,4) is missing one dimension. You need 1 int / float value for each pixel in image. Is 1400x2100 high/width ? – Shamit Verma Aug 22 '19 at 03:22
  • Regarding "But what if I increase one more channel in the image? " what would those new channels represent for each pixel? E.g: in RGBA : rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque). – Shamit Verma Aug 22 '19 at 03:24
  • 1
    Yes, 1400 is the height and 2100 is the width of the image. These images are supposed to be an individual class mask in the image segmentation task (Not to be confused with the image channels and native image encoding concepts). – thanatoz Aug 22 '19 at 05:15