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.
- Import Image, and ImageFilter modules from Pillow library.
- Read input image using Image.open() function.
- Convert image to RGB using Image.convert() function.
- 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]
Red channel [red_channel.jpg]
Green channel [green_channel.jpg]
Blue channel [blue_channel.jpg]
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.