简体   繁体   English

将 2d 转换为 3d 数组后保存图像问题

[英]Save image issue after converting 2d to 3d array

I have convert 2d array(arr2d) to 3d array(arr3d) and save as image using below code.我已将 2d 数组(arr2d)转换为 3d 数组(arr3d)并使用以下代码另存为图像。 arr2d is float64 type. arr2d 是 float64 类型。 Why save image is not color image?为什么保存图像不是彩色图像?

arr3d[:, :, 0] = arr3d[:, :, 1] = arr3d[:, :, 2] = arr2d
arr3d=arr3d*255
im=Image.fromarray(np.maximum(np.minimum(arr3d, 255), 0).astype(np.uint8))
im.save(“sample.png”)

The image is not color image because for every pixel red green and blue color channels have the same value.该图像不是彩色图像,因为对于每个像素,红色绿色和蓝色颜色通道具有相同的值。

When using im=Image.fromarray(arr3d) :使用im=Image.fromarray(arr3d)

  • arr3d[:, :, 0] is the red pixels plane. arr3d[:, :, 0]是红色像素平面。
  • arr3d[:, :, 1] is the green pixels plane. arr3d[:, :, 1]是绿色像素平面。
  • arr3d[:, :, 2] is the blue pixels plane. arr3d[:, :, 2]是蓝色像素平面。

When using arr3d[:, :, 0] = arr3d[:, :, 1] = arr3d[:, :, 2] = arr2d you are making sure that red=green=blue for every pixel.使用arr3d[:, :, 0] = arr3d[:, :, 1] = arr3d[:, :, 2] = arr2d您要确保每个像素的红色=绿色=蓝色

The color of pixel with R=G=B is gray . R=G=B 的像素颜色为灰色

I created the following code for reproducing the issue:我创建了以下代码来重现问题:

import numpy as np
from PIL import Image

arr2d = np.random.rand(50, 50) # Create 50x50 2D array with random values in range [0, 1]
arr3d = np.zeros((50, 50, 3))
arr3d[:, :, 0] = arr3d[:, :, 1] = arr3d[:, :, 2] = arr2d
arr3d = arr3d*255
im = Image.fromarray(np.maximum(np.minimum(arr3d, 255), 0).astype(np.uint8))
im.save("sample.png")

Result - all pixels are gray:结果 - 所有像素均为灰色:
在此处输入图片说明

Example for getting result with some colors:使用某些颜色获取结果的示例:

arr3d[:, :, 0] = np.random.rand(50, 50);arr3d[:, :, 1] = np.random.rand(50, 50);arr3d[:, :, 2] = np.random.rand(50, 50)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM