Mahotas – Creating RGB Image
In this article we will see how we can create a RGB image in mahotas. An RGB image, sometimes referred to as a truecolor image, is stored in MATLAB as an m-by-n-by-3 data array that defines red, green, and blue color components for each individual pixel. RGB image can be created with the help of array of each channel.
In order to do this we will use
as_rgbmethodAttention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Syntax : mahotas.as_rgb(r, g, b)
Argument : It takes three numpy array as argument
Return : It returns RGB ndarray object
Below is the implementation
# importing required librariesimport mahotasimport mahotas.demosfrom pylab import gray, imshow, showimport numpy as np # creating array of shape 50x50# for red channelr = np.arange(2500).reshape(50, 50) # for blue channelg = np.arange(2500).reshape(50, 50) # for blue channelb = np.arange(2500).reshape(50, 50) # making red channel values to 0r = r * 0 # increasing green channel valuesg = g * 100 # making blue channel values to 0b = b * 0 # creating rgb image from these three channelimg = mahotas.as_rgb(r, g, b) # showing imageimshow(img)show() |
Output :
Another example
# importing required librariesimport mahotasimport mahotas.demosfrom pylab import gray, imshow, showimport numpy as np # creating numpy linspacez1 = np.linspace(0, np.pi) # creating numpy meshgridX, Y = np.meshgrid(z1, z1) # creating rgb channels# creating red channel through sin functionred = np.sin(X) # creating green channel through cos functiongreen = np.cos(4 * Y) # creating blue channelblue = X * Y # creating rgb image from these three channelimg = mahotas.as_rgb(red, green, blue) # showing imageimshow(img)show() |
Output :


