from PIL import Image from torchvision import transforms #python的用法--》tensor数据类型 #通过transforms.ToTensor来看两个问题 #1. 如何使用transforms(python) #2. 为何需要Tensor数据类型 img_path = r"F:\PycharmProjects1\DLT\PracticeDataset\train\ants_image\0013035.jpg" img = Image.open(img_path) # print(img) #输出img信息<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x18D97553AF0> #1. 如何使用transforms(python)# tensor_trans=transforms.ToTensor() # tensor_img=tensor_trans(img) #上两行等同于tensor_img=transforms.ToTensor()(img) tensor_img=transforms.ToTensor()(img) print(tensor_img)