from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms img_path = r"C:\Users\12642\Desktop\Picture\dataset\train\ants\6240338_93729615ec.jpg" img = Image.open(img_path) writer = SummaryWriter("logs") # transforms.ToTensor() 将 PIL.Image 或者 numpy.ndarray 转化为 tensor tensor_trans = transforms.ToTensor() tensor_img = transforms.ToTensor()(img) writer.add_image("tensor_img", tensor_img) # 这里的tensor_img是一个三维的tensor,所以可以直接显示 writer.close()