图像不是彩色图像,因为对于每个像素,红色绿色和蓝色通道具有相同的值。
使用im=Image.fromarray(arr3d)时:
-
arr3d[:, :, 0] 是红色像素平面。
-
arr3d[:, :, 1] 是绿色像素平面。
-
arr3d[:, :, 2] 是蓝色像素平面。
使用arr3d[:, :, 0] = arr3d[:, :, 1] = arr3d[:, :, 2] = arr2d 时,您要确保每个像素都具有red=green=blue。
R=G=B 像素的颜色是灰色。
我创建了以下代码来重现该问题:
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")
结果 - 所有像素都是灰色的:
获取某些颜色的结果示例:
arr3d[:, :, 0] = np.random.rand(50, 50);arr3d[:, :, 1] = np.random.rand(50, 50);arr3d[:, :, 2] = np.random.rand(50, 50)