2013-07-08 52 views
0

我一直在尝试将RGB值的整数数组转换为PNG图像。我如何从下面的整数数组中生成以下图像?将整数数组转换为Python中的PNG图像

enter image description here

'''This is a 3D integer array. Each 1D array inside this array is an RGBA value''' 
'''Now how can I convert this RGB array to the PNG image shown above?''' 
rgbArray = [ 
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]], 
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]], 
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]], 
[[255,0,0], [255, 0, 0], [255, 0, 0], [255, 0, 0], [0,0,255], [0,0,255], [0,0,255], [0,0,255]], 
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]], 
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]], 
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]], 
[[0,0,255], [0,0,255], [0,0,255], [0,0,255], [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0]], 
] 
+1

而Python图像库PIL不适合你吗? –

+1

问题要求我们推荐一个工具,图书馆或最喜欢的非现场资源,因为它们倾向于吸引舆论的答案和垃圾邮件,所以不适合Stack Overflow。相反,请描述问题以及到目前为止解决问题所做的工作。 – Doorknob

+0

@ Doorknob Yikes ...恐怕您的评论会鼓励其他用户降低我的问题。 :(他们会假设我没有在解决我自己的问题上做出实质性的努力,并且他们会因为这个原因而低估我的问题。:到目前为止,我已经创建了一个RGB整数数组值,我问我应该怎么做才能从这个数组中创建一个PNG图像。这是否会导致我缺乏足够的研究工作? –

回答

5

您可以使用Python Imaging Library将RGB数据点转换为大多数标准格式。

from PIL import Image 

newimage = Image.new('RGB', (len(rgbArray[0]), len(rgbArray))) # type, size 
newimage.putdata([tuple(p) for row in rgbArray for p in row]) 
newimage.save("filename.png") # takes type from filename extension 

其产生:PIL output

.save()方法还可以采取一个格式参数,PNG将固定输出到PNG。

(我建议您安装Pillow fork,因为它更积极地维护并增加适当的打包和Python 3支持)。