TypeError: Cannot handle the data type in PIL Imag

2019-08-04 09:09发布

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

1条回答
神经病院院长
2楼-- · 2019-08-04 09:53

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))
查看更多
登录 后发表回答