RGB channels

Each pixels is (usually) a combination of red, green, and blue mixed together in various amounts. We can get these individual image bands using the split() method.

First we need to make sure that we are working with an RGB image. The mode method displays the mode (the type and depth of a pixel) of the image;

img = Image.open('handsome.jpg')
print(img.mode)

The result:

RGB

The split() method returns a tuple with the values of r,g, and b. Three new images are created, each containing a copy of either red, green, and blue channel.

r,g,b = img.split()

The values are stored in corresponding variables. To show only the image’s red channel, we can use the following line:

r.show()

The merge() function takes individual channels and combines them into a new image. It takes the mode and channels as parameters. We can specify different order of channels for some cool effects:

new_img = Image.merge('RGB', (g,b,r))
new_img.show()

Geek University 2022