I have convert 2d array(arr2d) to 3d array(arr3d) and save as image using below code. arr2d is float64 type. 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)
:
arr3d[:, :, 0]
is the red pixels plane. arr3d[:, :, 1]
is the green pixels plane. arr3d[:, :, 2]
is the blue pixels plane. When using arr3d[:, :, 0] = arr3d[:, :, 1] = arr3d[:, :, 2] = arr2d
you are making sure that red=green=blue for every pixel.
The color of pixel with R=G=B is gray .
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")
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.