Combine 3 separate numpy arrays to an RGB image in Python Combine 3 separate numpy arrays to an RGB image in Python numpy numpy

Combine 3 separate numpy arrays to an RGB image in Python


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

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


I don't really understand your question but here is an example of something similar I've done recently that seems like it might help:

# r, g, and b are 512x512 float arrays with values >= 0 and < 1.from PIL import Imageimport numpy as nprgbArray = np.zeros((512,512,3), 'uint8')rgbArray[..., 0] = r*256rgbArray[..., 1] = g*256rgbArray[..., 2] = b*256img = Image.fromarray(rgbArray)img.save('myimg.jpeg')

I hope that helps


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

This code doesnt create 3d array if you pass 3 channels. 2 channels remain.