在 Python 中将 3 个单独的 numpy 数组组合到一个 RGB 图像 [英] Combine 3 separate numpy arrays to an RGB image in Python

查看:22
本文介绍了在 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.

我尝试使用图像"来完成这项工作,但它需要归因于模式".

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

我尝试了一个技巧.我会使用 Image.fromarray() 将数组转换为图像,但当 Image.merge 需要L"模式图像合并时,默认情况下它会获得F"模式.如果我首先将 fromarray() 中的数组属性声明为 'L',那么所有的 R G B 图像都会失真.

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.

推荐答案

rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3

还要将浮点数 0 .. 1 转换为 uint8 s,

To also convert floats 0 .. 1 to uint8 s,

rgb_uint8 = (np.dstack((r,g,b)) * 255.999) .astype(np.uint8)  # right, Janna, not 256

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆