TypeError: Cannot handle the data type in PIL Image
I have a Pytorch tensor of size (4,3,224,224). When I am trying to convert the first tensor into an Image object, it says:

TypeError: Cannot handle this data type


I ran the following command:

img = Image.fromarray(data[0][i].numpy().astype(np.uint8))


where data is the Pytorch tensor

I tried other solutions but couldn't find any solution.

Please suggest !!

You are trying to convert 3x224x224 np.array into an image, but PIL.Image expects its images to be of shape 224x224x3, threfore you get an error.
If you transpose your tensor so that the channel dimension will be the last (rather than the first), you should have no problem

img = Image.fromarray(data[0][i].transpose(0,2).numpy().astype(np.uint8))

