在Python中将3个单独的numpy数组合并为RGB图像

在Python中将3个单独的numpy数组合并为RGB图像

本文介绍了在Python中将3个单独的numpy数组合并为RGB图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一组数据可以转换成R,G,B波段的单独numpy数组。现在我需要将它们组合起来形成RGB图像。

So I have a set of data which I am able to convert to form separate numpy arrays of R, G, B bands. Now I need to combine them to form an RGB image.

我尝试'Image'来完成这项工作,但需要'模式'来归因。

I tried 'Image' to do the job but it requires 'mode' to be attributed.

我试图做一个技巧。我会使用Image.fromarray()将数组带到图像,但默认情况下,当Image.merge需要L模式图像进行合并时,它会达到'F'模式。如果我在第一个位置将fromarray()中的数组属性声明为'L',则所有RGB图像都会失真。

I tried to do a trick. I would use Image.fromarray() to take the array to image but it attains 'F' mode by default when Image.merge requires 'L' mode images to merge. If I would declare the attribute of array in fromarray() to 'L' at first place, all the R G B images become distorted.

但是,如果我保存图像和然后打开然后合并,它工作正常。图像以L模式读取图像。

But, if I save the images and then open them and then merge, it works fine. Image reads the image with 'L' mode.

现在我有两个问题。

首先,我不认为这是一种优雅的工作方式。因此,如果有人知道更好的方法,请告诉

First, I dont think it is an elegant way of doing the work. So if anyone knows the better way of doing it, please tell

其次,Image.SAVE无法正常工作。以下是我面临的错误:

Secondly, Image.SAVE is not working properly. Following are the errors I face:

In [7]: Image.SAVE(imagefile, 'JPEG')
----------------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/media/New Volume/Documents/My own works/ISAC/SAMPLES/<ipython console> in <module>()

TypeError: 'dict' object is not callable

请建议解决方案。

请注意图像大小在4000x4000左右。

And please mind that the image is around 4000x4000 size array.

推荐答案

我真的不明白你的问题,但这里有一个类似我最近做过的事情的例子似乎可能有所帮助:

I don't really understand your question but here is an example of something similar I've done recently that seems like it might help:

# r, g, and b are 512x512 float arrays with values >= 0 and < 1.
from PIL import Image
import numpy as np
rgbArray = np.zeros((512,512,3), 'uint8')
rgbArray[..., 0] = r*256
rgbArray[..., 1] = g*256
rgbArray[..., 2] = b*256
img = Image.fromarray(rgbArray)
img.save('myimg.jpeg')

我希望有帮助

这篇关于在Python中将3个单独的numpy数组合并为RGB图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:58