Bands and Modes of an Image
For example, in an RGB image, each pixel is represented by three values corresponding to the red, green, and blue values for that pixel. Therefore, the
Image object for an RGB image contains three bands, one for each color.
00:45 An RGBA image has four bands, one for each of the colors, and a fourth one containing those alpha values. Each band has the same dimensions as the image dimensions, and therefore, an RGBA image of size 100x100 pixels is represented by a 100x100x4 array of values.
You can see the full list of supported modes in the Pillow documentation. You can find out how many bands and image object contains by using the
.getbands() method, and you can convert between modes using
You can separate an image into its bands using
.split(), and this method returns all the bands as separate
Image objects. You can confirm this by displaying the string representation of one of the objects that was returned.
Now you can create three new RGB images showing the red, green, and blue channels separately using
merge(), which is a function in the
Image module. To create a band containing zeros everywhere, you use the
.point() needs a function as an argument. The function that you use determines how each point transforms. In this case, you use a lambda function to map each point to
0 to create the image showing only the red channel.
So here you use the red channel from the original image and two bands of zeros for the green and blue channels. When you merge the red band with green and blue bands containing zeros, you get an RGB image called
red_merge. Therefore, the RGB image that you create only has non-zero values in the red channel, but because it’s an RGB image, it will display in color.
05:03 The green and blue channels show these pixels as dark because they have small values. The exceptions are those pixels that represent the reflection of the light on the surface of the strawberry as these pixels are nearly white.
Become a Member to join the conversation.