PIL,cv2,plt读取和显示图像;组合多幅图像
1.PIL
(1) 用image.show()
和image.save('PIL1.jpg')
直接显示和保存PIL格式的image
(2) transforms.Compose.ToTensor()
把PIL图像或者数组图像numpy.ndarry(WxHxC)从[0,255]转换到float型[0.0,1.0]的tensor;
(3) transforms.Compose.Normalize(Tensor,mean,std,inplace),比如transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
说明三个通道的mean和std均是0.5.
(4) 保存arrayImage.fromarray(image).save('PIL.jpg')
array通过transforms.Compose转换为PIL后保存,unloader = transforms.ToPILImage() image = (unloader(image))
transforms.Compose.Resize()
只能对PIL图像进行转换
(5) 结果如下:
PIL1和PIL显示
from PIL import Image
import torchvision.transforms as transforms
import numpy as np
image_path = 'E:/aboutVS/dataset/image/LIVE/gblur/img2.bmp'
transform = transforms.Compose([
transforms.Resize(256),
transforms.RandomResizedCrop(224,scale=(0.5,1.0)),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
]) #定义变换:resize+随机裁剪+totensor+归一化
im = Image.open(image_path,"r").convert('RGB') # 读取为PIL图像
im.show() # 直接显示PIL图像
image = (transform(im)).permute(1,2,0) # 变为tensor
image = (image.data.numpy()*255).astype(np.uint8) # array
Image.fromarray(image).save('PIL.jpg') # 把array显示为图像
unloader = transforms.ToPILImage() # array转换成PIL
image = (unloader(image))
image.show()
image.save('PIL1.jpg') # 保存PIL图像
2. plt
(1) 用python mping.imread(image_path)
读取得到array格式的图像;pythontransforms.Compose
会改变array的维度(WxHxC到CxWxH),所以之后需要用im = im.permute(1,2,0)
恢复WxHxC;
(2) 通过im = (im.data.numpy()*255.).astype(np.uint8)
将tensor转换为array,可以用misc.toimage(im).save('array.png')
或Image.fromarray(im).save('array2.png')
将array保存为图像;
(3) 用plt.imshow(im)
绘制im,plt.savefig('plt.png')
保存绘制的图像而非im,最后通过plt.show()
实现可视化。
import torchvision.transforms as transforms
import matplotlib.image as mping # 用于matlab读取图像
import matplotlib.pyplot as plt # 用于matlab显示图像
import numpy as np
from PIL import Image
from scipy import misc
image_path = 'E:/aboutVS/dataset/image/LIVE/gblur/img2.bmp'
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
]) # transforms.Resize(256)的操作对象是PIL图像,所以先转换为PIL图像,再转换成tensor
# matplotlib的读取,显示,保存图像
im = mping.imread(image_path) # W*H*C,array
im = transform(im) # tensor,C*W*H
im = im.permute(1,2,0) # permute()对tensor的维度做转换
im = (im.data.numpy()*255.).astype(np.uint8) # array
misc.toimage(im).save('array.png') # 将array保存为图像
Image.fromarray(im).save('array2.png') # 将array保存为图像
# 可视化
plt.imshow(im) # plt.imshow(im,cmap='gray'),显示灰度图
plt.axis('off') # 不显示坐标轴
plt.savefig('plt.png') # 把matplotlib画出的图像保存
plt.show()
plt绘制图像如下:
3. cv2
import torchvision.transforms as transforms
import numpy as np
import cv2
image_path = 'E:/aboutVS/dataset/image/LIVE/gblur/img2.bmp'
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
])
# cv2的读取,显示,保存图像
img = cv2.imread(image_path) # image转换为array,W*H*C
img = transform(img) # array变为tensor,C*W*H
img = (img.numpy()*255).astype(np.uint8) # tensor变为array,C*W*H
img = np.transpose(img,(1,2,0)) # array,W*H*C,np.transpose()转换array的维度
cv2.imshow('img', img)
cv2.imwrite('practise/cv2.jpg', img)
cv2.waitKey(0)
cv2图像
4. 不同图像组合
通过os.path.join()
组合图像路径,Image.open
读取图像,im.crop(x,y,z,r)对图像进行裁剪,裁剪位置由box1的参数控制;ref.paste()
对图像进行组合,依次粘贴裁剪过的图像到对应位置,位置由box2的参数控制。
"""
组合图像
"""
from PIL import Image
from argparse import ArgumentParser
import os
paraser = ArgumentParser(description="compose four different distortion")
paraser.add_argument("--path",type=str,default='E:/aboutVS/dataset/image/LIVE/wn/')
args = paraser.parse_args()
name_list = ('img173.bmp','img34.bmp','img1.bmp','img17.bmp')
ref = Image.open(os.path.join(args.path,name_list[3]))
for i in range(len(name_list)):
im = Image.open(os.path.join(args.path,name_list[i]))
w,h = im.size
box1 = (int((w*i)/4),0,int((w*(i+1))/4),h)
im = im.crop(box1)
box2 = (int((w*i)/4),0,int((w*i)/4)+im.width,im.height)
ref.paste(im, box2)
ref.show() # 显示
# im.save()不能组合路径+文件名
ref.save('combination.png') #保存
结果如下: