1.png
2.png
在pytorch中使用torchvision的vutils函数实现对多张图片的拼接。具体操作就是将上面的两张图片,1.png和2.png的多张图片进行拼接形成一张图片,拼接后的效果如下图。
给出具体代码:
import matplotlib.pyplot as plt from PIL import Image import numpy as np import torch import torchvision.utils as vutils im1=Image.open("1.png").convert("RGB") im1 = im1.resize((1000, 1000)).rotate(-90) im2=Image.open("2.png").convert("RGB") im2 = im2.resize((1000, 1000)).rotate(-90) # 1000, 1000, 3 => 3, 1000, 1000 images = [np.moveaxis(np.array(im1), 2, 0), np.moveaxis(np.array(im2), 2, 0)]*8 images_tensor = vutils.make_grid(torch.tensor(images)/255.0, nrow=4, padding=0, normalize=True) print(images_tensor.shape) # 3, 1000, 1000 => 1000, 1000, 3 plt.imshow(images_tensor.numpy().transpose((1,2,0))) plt.show() vutils.save_image(images_tensor, "3.png") vutils.save_image(images_tensor, "3_back.png", nrow=2, padding=0, normalize=True) vutils.save_image(torch.tensor(images)/255.0, "4.png", nrow=8, padding=0, normalize=True)
=============================================
标签:tensor,torchvision,utils,vutils,images,import,png,1000 From: https://www.cnblogs.com/devilmaycry812839668/p/16721875.html