用 cv2 或 Pillow 保存 numpy 格式图片
img = np.asarray(img)
if len(img.shape) == 4:
assert img.shape[0] == 1
img = img[0]
assert len(img.shape) == 3 and img.shape[0] in (1, 3)
img = img.transpose(1, 2, 0) # to channels-last
#1
img = img[..., ::-1] # RGB to BGR
img = (img * 255.0).astype(np.uint8)
img = np.ascontiguousarray(img)
assert cv2.imwrite("fused.jpg", img)
#2
## https://pytorch.org/vision/main/generated/torchvision.transforms.ToPILImage.html
## img.shape: (C x H x W) if img is torch.Tensor, (H x W x C) if img is np.ndarray.
# img = transforms.ToPILImage()(img)
# img.save('fused.jpg')
要么进行 RGB -> BGR、将 0~1 缩放到 0~255、转换到 np.uint8
类型、将数组转连续等操作,要么直接使用 torchvision 的 transforms 工具。