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

在Python中,用户遇到了将三个独立的numpy数组(R、G、B通道)合并成RGB图像的问题。尝试使用PIL库的`Image`,但发现需要指定'mode'。直接使用`Image.fromarray()`会默认设置为'F'模式,而`Image.merge`需要'L'模式。通过保存并重新打开图像可以解决模式问题,但认为这不是优雅的方法。同时,使用`Image.SAVE`保存图像时遇到TypeError。解决方案是将数组乘以256并将数据类型转换为'uint8',然后使用`Image.fromarray()`创建图像并保存。

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.

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.

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

Now I have two issues.

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

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

In [7]: Image.SAVE(imagefile

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值