Digital art in Python: decomposing an image in its color channels

digital image processing python software development Aug 03, 2023

Python is an extremely versatile language that allows us to do practically anything, from creating web applications to artificial intelligence, and of course, it allows us to easily manipulate images. In this article we will see how in just a few lines of code we can break down an image into its main components, and put it back together.


To learn Python and build the skills to land your first job, check out our Professional Python Developer Bootcamp.


How computers represent images

Computers represent color through a combination of numbers. Although there are many different ways to represent the same colors, the most common is the RGB model, according to which a color is represented with three values: one for the intensity of red, another for the intensity of green, and the last one for the intensity of blue.

Each of these values can range from 0 (the lowest intensity) to 255 (the highest). In the following image you can see different colors with their intensities:




A digital image is nothing more than a matrix with rows and columns formed by color dots, called pixels. Each pixel has an associated RGB value (that is, three intensity values for red, green and blue). If we zoom out far enough from the pixel matrix, we can see the image they represent. For example, the following image has a resolution of 281 columns of pixels by 500 rows:



Image decomposition in Python

One of the advantages of Python is that it has tools to work in many domains, including digital photography. To demonstrate how to easily manipulate images, we are going to break down the previous image into each of the colors that our computer uses to represent it.

To do this, we will start by importing PIL (Python Imaging Library) and loading the previous image into a variable:

Now, from that image we are going to extract only the red value of each pixel, forming a new image the same size as the original, but with a single value per pixel (the intensity of the red color). Then, we will also extract the intensity of the green and the intensity of the blue.

In this way we will have three different images, each one representing the intensity of each color. In Python, we can use the split() method of the Image class to obtain those three images from the original. Then, we just need to display each of those images with the show() method to see them:


As you can see, Python gives us the color channels of the original image in black and white. The reason is that the pixels in these images, instead of having three colors, only have one. Python thinks these are black and white images, where the value of each pixel is the intensity of the light. However, we know that is not the case. Each image corresponds to the intensity of a color in the original image.

To fix this, we are going to make Python display these images in color again. The difference is that now, the first image will only show the red color, the second one the green, and the third one the blue. To do this, for each pixel in the red image we will add the two missing values for green and blue, which will be 0. For the pixels in the green image we will add 0 for the red and blue values. Finally, for the blue image we will add zero to each pixel for the red and green values.



By doing this, Python will understand that these are color images. To achieve this, we will create a new image with the value 0 in all its pixels (black), and we will use it to fill in the values of the colors that the previous images don’t need:


And as you can see above, the result is our original image broken down into each of its colors. And, in fact, if we mix these three images again, we can reconstruct the original image:


AI moves fast. We help you keep up with it.

Get a monthly selection of the most groundbreaking advances in the world of AI, top code repositories, and our best articles and tutorials. 

We hate SPAM. We will never sell your information, for any reason.