将三张灰度图转换为RGB三通道图片python

import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
#使用cv2读取图片
img = cv2.imread('1.png',0)
print(img.shape)
img1 = cv2.imread('2.png',0)
print(img1.shape)
img2 = cv2.imread('3.png',0)
print(img2.shape)
#将所有图片准换为同样大小
img = np.resize(img,(255,64))
print(img.shape)
img1 = np.resize(img1,(255,64))
print(img1.shape)
img2 = np.resize(img2,(255,64))
print(img2.shape)

com = np.array([img, img1, img2])
print('合并矩阵:\n', com)
print('维数:', com.shape)
com=np.swapaxes(com,2,0)#转换三维矩阵X,Z轴的位置,将3放到Z轴上,这样才可以直接转换为RGB三色图
com=np.swapaxes(com,0,1)#将X,Y轴调换位置,输出图片就是垂直方向的了
print('维数:', com.shape)
com = Image.fromarray(com).convert('RGB')  # 将数组转化回图片s
com.save("out.png")  # 将数组保存为图片

你可能感兴趣的:(python,计算机视觉,opencv)