numpy转pillow图像报错TypeError: Cannot handle this data type: (1, 1, 134), <f4 TypeError: Cannot handle this data type:

发布时间 2023-08-29 09:06:15作者: 海_纳百川

报错TypeError: Cannot handle this data type: (1, 1, 134), <f4,我猜你很可能是在将array数据转换成图片,使用的是函数

Image.fromarray()

而这个函数处理的是uint8类型,所以你可以使用:

print(image.dtype)

查看数据类型,不是uint8格式就转换成uint8:Image.fromarray(np.uint8(image))

然后接下来很可能还会出现错误:TypeError: Cannot handle this data type: (1, 1, 134), |u1

这是因为要将uint8类型的数据转换为PIL类型的图片的时候,格式为(W,H,C)而tensor格式的数据类型是(C,W,H)所以可以使用一下函数查看格式:

print(img.shape)

修正:

img=image.transpose(1,2,0)

该函数和torch.permute()函数作用相似,将shape(0)位置移到shape(2)位置,比如原来是[3,256,256],对调后是[256,,62,3]

造成以上错误可能是你读取的是tensor类型的数据,也有可能你使用了切片的方式进行读取的数据