0

我正在尝试通过合并三个灰度 png 图像来创建一个 RGB png 图像pypng。我已将 png 文件读入 numpy 数组,如下所示

pngFile1 = png.Reader("file1.png").read()
pngFile2 = png.Reader("file2.png").read()
pngFile3 = png.Reader("file3.png").read()

pngArray1 = np.array(list(pngFile1[2]))
pngArray2 = np.array(list(pngFile2[2]))
pngArray3 = np.array(list(pngFile3[2]))

如何组合这三个数组/图像来重新创建 RGB png 图像?

4

3 回答 3

2

我发现scipy可以将灰度 png 直接读取到数组中。

from scipy import misc
import numpy

R = misc.imread("r.png")
G = misc.imread("g.png") 
B = misc.imread("b.png") 

RGB = numpy.zeros((R.shape[0], R.shape[1], 3), "uint8") 
RGB [:,:,0] = R
RGB [:,:,1] = G
RGB [:,:,2] = B

misc.imsave("rgb.png", RGB)
于 2015-05-04T13:14:24.160 回答
1

为简单起见,让我们假设您有 3 个3x3尺寸的灰度图像,并将这 3 个灰度图像表示为:

import numpy as np 

R = np.array([[[1], [1], [1]], [[1], [1], [1]], [[1], [1], [1]]])
G = np.array([[[2], [2], [2]], [[2], [2], [2]], [[2], [2], [2]]])
B = np.array([[[3], [3], [3]], [[3], [3], [3]], [[3], [3], [3]]])
RGB = np.concatenate((R,G,B), axis = 2)

print RGB

>>> [[[1 2 3]
      [1 2 3]
      [1 2 3]]

     [[1 2 3]
      [1 2 3]
      [1 2 3]]

     [[1 2 3]
      [1 2 3]
      [1 2 3]]]
于 2015-05-04T11:42:23.830 回答
0

我应该创建一个二维数组,每行3*width包含连续的 RGB 值

pngFile1 = png.Reader("file1.png").read()
pngFile2 = png.Reader("file2.png").read()
pngFile3 = png.Reader("file3.png").read()

pngArray1 = np.array(list(pngFile1[2]))
pngArray2 = np.array(list(pngFile2[2]))
pngArray3 = np.array(list(pngFile3[2]))

//get dimension, assuming they are the same for all three images
width = pngArray1[0]
height = pngArray1[1]

//create a 2D array to use on the png.Writer
pngArray = np.zeros([height, 3*width])

for i in range(height)
    for j in range(width)
        pngArray[i][j*3 + 0] =  pngArray1[i][j]
        pngArray[i][j*3 + 1] =  pngArray2[i][j]
        pngArray[i][j*3 + 2] =  pngArray3[i][j]

fileStream = open("pngFileRGB.png", "wb")
writer = png.Writer(width, height)
writer.write(fileStream, pngArray)
fileStream.close()
于 2015-05-04T12:25:04.777 回答