Python imaging library 转换为PIL时图像颜色不正确

Python imaging library 转换为PIL时图像颜色不正确,python-imaging-library,tensor,Python Imaging Library,Tensor,我正在将张量转换为pil图像,但颜色是扭曲的。我已经尝试将其转换为BGR,但结果仍然是一样的 trsn = transforms.ToPILImage() temp = (target + torch.mean(target)) *torch.std(target) res = trsn(temp.to('cpu').detach().squeeze()) np_img=np.array(res) bgr = cv2.cvtColor(np_img,cv2.COLOR_RGB2BGR) pil_

我正在将张量转换为pil图像,但颜色是扭曲的。我已经尝试将其转换为BGR,但结果仍然是一样的

trsn = transforms.ToPILImage()
temp = (target + torch.mean(target)) *torch.std(target)
res = trsn(temp.to('cpu').detach().squeeze())
np_img=np.array(res)
bgr = cv2.cvtColor(np_img,cv2.COLOR_RGB2BGR)
pil_im = Image.fromarray(bgr)
print(res)
# plt.imshow(res)
plt.imshow(res,label="Epoch "+str(i))
plt.show()
# im=Image.fromarray(np.uint8(target.to('cpu').detach().numpy()*255))
pil_im.save('f.jpeg')
我有一个将张量转换为numpy的函数,可以在matplotlib上显示图像

下面是函数

def imcnvt(image):
    x = image.to("cpu").clone().detach().numpy().squeeze()
    x = x.transpose(1,2,0)
    x = x*np.array((0.5,0.5,0.5)) + np.array((0.5,0.5,0.5))
    return torch.Tensor(x)
这是我得到的照片

这是我想要的PIL照片


PIL使用RGB排序,因此在从Numpy数组创建PIL映像之前,不应将其转换为BGR。PIL使用RGB排序,因此在从Numpy数组创建PIL映像之前,不应将其转换为BGR。