首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用三个灰度png文件创建RGB图像- pypng

使用三个灰度png文件创建RGB图像- pypng
EN

Stack Overflow用户
提问于 2015-05-04 19:17:39
回答 3查看 1.3K关注 0票数 0

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

代码语言:javascript
复制
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图像?

EN

回答 3

Stack Overflow用户

发布于 2015-05-04 21:14:24

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

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2015-05-04 19:42:24

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

代码语言:javascript
复制
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]]]
票数 1
EN

Stack Overflow用户

发布于 2015-05-04 20:25:05

我应该创建一个2D数组,每行都是3*width,并且包含连续的RGB值

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30028796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档