Splitting RGB channels in Python

Image processing is a crucial part of computer vision and is aided by numerous Python libraries. Computer vision, in easier words, is a field of artificial intelligence containing techniques that allow machines to analyze and interpret images and videos, recognize objects, and extract information from visual data.

It can range from very simple to highly complex tasks, and in almost all of them, some preprocessingtransformation of raw data before it is used is required. This preprocessing could be related to normalization, colors, or sizes. In this Answer, we'll be looking at the RGB aspect of images and how to split an RGB channel.

Splitting RGB channels
Splitting RGB channels

RGB channels

In computer vision and image processing, RGB i.e. red, green, and blue, is a common color model we use to represent images. Splitting RGB channels involves separating the red, green, and blue components of an image. This effectively allows us to view the intensities of the colors individually and manipulate them.

  1. Red channel: The red channel represents the intensity of the red color component in an image. By isolating it, we can detect red objects in computer vision applications.

  2. Green channel: The green channel represents the intensity of the green color component. We can apply this in nature-oriented images, enabling the exploration of green-dominant regions.

  3. Blue channel: The blue channel represents the intensity of the blue color component. This could be useful in scenarios like underwater imaging etc.

RGB colors
RGB colors

RGB values

RGB values are usually represented as a 3-dimensional vector like (R, G, B), where each component ranges from 0 to 255. In coding, we represent them as 3 integers within brackets, like (255, 0, 0) for red, (0, 255, 0) for green, and (0, 0, 255) for blue, respectively.

Matplotlib and PIL

In Python, libraries like PIL i.e. Python Imaging Library and Matplotlib, facilitate easy access to RGB channels, image processing, and plotting. In our answer, we will be using these libraries together.

Python library logos
Python library logos

Displaying split channels

For simply splitting the RGB channels in an image, we can use the cmaps function of Matplotlib to display the specific channels.

  • cmaps = 'Reds' is used for displaying the red channel.

  • cmaps = 'Blues' is used for displaying the blue channel.

  • cmaps = 'Greens' is used for displaying the green channel.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

img = Image.open("test_image.png")
M = np.asarray(img)

plt.figure(figsize=(12, 6))
plt.subplot(131)

plt.imshow(M[:, :, 0], cmap='Reds', vmin=0, vmax=255)
plt.title("Red Channel")
plt.subplot(132)

plt.imshow(M[:, :, 1], cmap='Greens', vmin=0, vmax=255)
plt.title("Green Channel")
plt.subplot(133)

plt.imshow(M[:, :, 2], cmap='Blues', vmin=0, vmax=255)
plt.title("Blue Channel")

plt.show()

Code explanation

  1. First, we import the necessary modules needed for our code to run properly.

  2. We load our image named test_image.png using the Image.open() method from PIL and save it in img.

  3. Next, we convert the loaded image img into a NumPy array M using M = np.asarray(img). Now, M represents the image as a 3D array, where each element represents a pixel's RGB color value.

  4. We then create a Matplotlib figure with a size of 12 x 6 inches using plt.figure(figsize=(12, 6)).

  5. Three subplots are created side by side using plt.subplot(131), plt.subplot(132), and plt.subplot(133). These three subplots represent the positions of the channels.

  6. For each subplot, we use plt.imshow() to display the channels.

    1. plt.imshow(M[:, :, 0], cmap='Reds', vmin=0, vmax=255): This displays the red channel i.e. index 0 of the array M with a colormap of "Reds". The vmin and vmax arguments set the range of colors for the colormap, which is from 0 to 255 i.e. minimum to maximum.

    2. plt.imshow(M[:, :, 1], cmap='Greens', vmin=0, vmax=255): We do the same for the green channel.

    3. plt.imshow(M[:, :, 2], cmap='Blues', vmin=0, vmax=255): We do the same for the blue channel.

  7. plt.title("Red Channel"), plt.title("Green Channel"), and plt.title("Blue Channel") set the titles of the three subplots to "Red Channel," "Green Channel," and "Blue Channel," respectively.

  8. We finally display our subplot figures using plt.show(), with each subplot showing one of the RGB color channels of the image.

Code output

The above code displays the three channels and labels them accordingly.

Cat image split into different channels
Cat image split into different channels

Displaying split and original images together

We can also create a figure containing the original image for comparison. The axis can be turned off too. Feel free to experiment with the code and try it by clicking "Run".

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

img = Image.open("test_image.png")
M = np.asarray(img)

plt.figure(figsize=(12, 12))

plt.subplot(2, 2, 1)
plt.imshow(img)
plt.title("Original Image")
plt.axis('off')

plt.subplot(2, 2, 2)
plt.imshow(M[:, :, 0], cmap='Reds', vmin=0, vmax=255)
plt.title("Red Channel")
plt.axis('off')

plt.subplot(2, 2, 3)
plt.imshow(M[:, :, 1], cmap='Greens', vmin=0, vmax=255)
plt.title("Green Channel")
plt.axis('off')

plt.subplot(2, 2, 4)
plt.imshow(M[:, :, 2], cmap='Blues', vmin=0, vmax=255)
plt.title("Blue Channel")
plt.axis('off')

plt.show()

Code output

The above code displays the original image and the three channels as four subplots.

Original image and split channels
Original image and split channels

Application scenario

Such a splitting task is inherently simple, but there are many more complex applications it can be used in. Let's take an interesting scenario below. Suppose we have an image and want to enhance one particular channel from the RGB channel. We can easily do that through the code below.

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image


img = Image.open("test_image.png")
M = np.asarray(img)

red_channel = M[:, :, 0]
green_channel = M[:, :, 1]
blue_channel = M[:, :, 2]

red_enhanced = np.clip(red_channel * 1.5, 0, 255)  

enhanced_image = np.stack((red_enhanced, green_channel, blue_channel), axis=-1).astype(np.uint8)
enhanced_image = Image.fromarray(enhanced_image)

plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(img)
plt.title("Original Image")

plt.subplot(1, 2, 2)
plt.imshow(enhanced_image)
plt.title("Enhanced Image (Red Channel)")

plt.show()

Code explanation

  1. We first load our image test_image.png using PIL and convert it into an array M.

  2. The red, green, and blue channels of the image are extracted into separate arrays called red_channel, green_channel, and blue_channel.

  3. This is the main image enhancement step. The intensity of the red channel is enhanced by multiplying all its values by 1.5 and then changing the result to stay within the valid intensity range i.e. 0 to 255.

  4. The enhanced red channel is combined with the original green and blue channels to create a new image enhanced_image.

  5. We create two subplots to display the original and enhanced images side by side and add titles to them.

  6. The Matplotlib figure containing both of the subplots is displayed using plt.show().

Code output

It's visible how the original image's red channel is enhanced below. Pretty good results, right?

Enhancing the red channel
Enhancing the red channel

Enhancing other channels

We can also enhance the green and blue channels using the same procedure.

Enhancing the green channel
Enhancing the green channel
Enhancing the blue channel
Enhancing the blue channel

Test your RGB knowledge here!

Question

What color does (0,0,255) represent in the RGB channel?

Show Answer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved