Access RGB channels of Image in Pillow


Pillow - Access RGB channels of image

To access RGB channels of an image using Pillow library, you can use Image.split() function.

Steps to access RBG Color channels of an image

Steps to get the RGB channels of a given image.

  1. Import Image, and ImageFilter modules from Pillow library.
  2. Read input image using Image.open() function.
  3. Convert image to RGB using Image.convert() function.
  4. Split the RGB image to channels using Image.split() function.

Examples

1. Get RGB channels in image

In the following example, we read an image test_image.jpg, get the RGB color channels in the image, and then save the individual channels as separate images.

Since we are saving channels, each of the color channel images appear as grey scale images, but it their corresponding color channel strength.

Python Program

from PIL import Image

# Open the image
image = Image.open("test_image.jpg")

# Convert the image
image = image.convert("RGB")

# Split image to channels
r, g, b = image.split()

# Save the channels to separate images
r.save("red_channel.jpg")
g.save("green_channel.jpg")
b.save("blue_channel.jpg")

Original Image [test_image.jpg]

Pillow - Access RGB channels of image

Red channel [red_channel.jpg]

Red channel of image

Green channel [green_channel.jpg]

Green channel of image

Blue channel [blue_channel.jpg]

Blue channel of image

Summary

In this Python Pillow Tutorial, we learned how to access the RGB channels of a given image using PIL.Image.split() function, with the help of examples.