python 将多张图片拼成一张 cv2.imread和image.open

from os import listdir
from PIL import Image

predata = Image.open(“samples/A/”+itemout)
afterdata = Image.open(“samples/B/”+itemout)
gtdata = Image.open(“samples/label/”+itemout)
predict = Image.open(“samples/output/”+itemout)
imslst = [predata,afterdata,gtdata,predict]

(注意:使用Image.open读取图片时,print(image.size)得到的是图片的真实维度:W * H(宽和高) )

width, height = imslst[0].size
result = Image.new(‘RGBA’, ((width+10)* len(imslst), height ))
(此句相当于创建一个画布,'RGBA’表示底色是透明的)

for i, im in enumerate(imslst):
result.paste(im, box=(i * width+i*10,0))
(box=(x,y):图片im在画布result上的位置)

保存:
result.save(“DEMO_RES/res.png”)

关于image.open:
参考:https://www.cnblogs.com/haifwu/p/12934003.html

image= image.open(“****”)
image= np.array(predata)
image.shape : H * W * C

需要用img=np.array(img)做转换,才能看到shape属性,是(height,width,channel)数组,channel的通道顺序为RGB。

import torchvision.transforms as transforms
image 转tensor:
input_transform = transforms.Compose([
transforms.ToTensor(),
])
image = input_transform(image).unsqueeze(0)

image.shape: 1* C * H * W

cv2.imread:
返回值是(height,width,channel)数组,channel的顺序是BGR顺序。
参考:https://www.jb51.net/article/187938.htm

你可能感兴趣的:(python 将多张图片拼成一张 cv2.imread和image.open)