将图像读取为tensor,把tensor存为图像

图像读取为tensor

def img2tensor(path):
    import torchvision.transforms as transforms
    import cv2 as cv
    img = cv.imread(path)
    transf = transforms.ToTensor()
    img_tensor = transf(img)
    # print('opencv', img)
    # print('torch', img_tensor)
    return img_tensor

tensor存为图像


def tensor2img(img,name):
    from torchvision import utils as vutils
    vutils.save_image(img, name, normalize=True)

猜你喜欢

转载自blog.csdn.net/swx595182208/article/details/130061008