gpt4 book ai didi

python - 类型错误 : Cannot handle the data type in PIL Image

转载 作者:行者123 更新时间:2023-11-30 22:09:14 38 4
gpt4 key购买 nike

我有一个大小为 (4,3,224,224) 的 Pytorch 张量。当我尝试将第一个张量转换为图像对象时,它说:

TypeError: Cannot handle this data type

我运行了以下命令:

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

其中 data 是 Pytorch 张量

我尝试了其他解决方案,但找不到任何解决方案。

请推荐!!

最佳答案

您正在尝试将 3x224x224 np.array 转换为图像,但 PIL.Image 期望其图像的形状为 224x224x3,因此您会收到错误。< br/>如果您转置张量以使 channel 维度成为最后一个(而不是第一个),那么应该没有问题

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

关于python - 类型错误 : Cannot handle the data type in PIL Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51979554/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com