Bands and Modes of an Image
00:00 Bands and Modes of an Image in the Pillow Library. An image is a two-dimensional array of pixels where each pixel corresponds to a color. Each pixel can be represented by one or more values.
00:13
For example, in an RGB image, each pixel is represented by three values corresponding to the red, green, and blue values for that pixel. Therefore, the Image
object for an RGB image contains three bands, one for each color.
00:28 An RGB image of size 100x100 pixels is represented by a 100x100x3 array of values. RGBA images also include the alpha value, which contains information about the transparency for each pixel.
00:45 An RGBA image has four bands, one for each of the colors, and a fourth one containing those alpha values. Each band has the same dimensions as the image dimensions, and therefore, an RGBA image of size 100x100 pixels is represented by a 100x100x4 array of values.
01:07 The mode of an image describes what kind of array you are working with. Pillow supports most standard modes, including black-and-white (binary), grayscale, RGB, RGBA, and CMYK.
01:21
You can see the full list of supported modes in the Pillow documentation. You can find out how many bands and image object contains by using the .getbands()
method, and you can convert between modes using .convert()
.
01:36
You’ll be using the image seen on-screen from the course materials for this part of the course. Start by importing Image
from PIL
and load the file as seen previously.
01:54
The original image’s mode is RGB, but you call .convert()
twice, once to convert from RGB to CMYK
02:04 and once to create a grayscale version.
02:09 The CMYK image looks similar to the original image, but is encoded using the mode that’s common for printed material rather than digital displays.
02:20 The conversion to grayscale gives the output seen on-screen.
02:27
The outputs from the calls to .getbands()
confirm that there are three bands in the RGB image, four bands in the CMYK image, and one band in the grayscale image.
02:42
You can separate an image into its bands using .split()
, and this method returns all the bands as separate Image
objects. You can confirm this by displaying the string representation of one of the objects that was returned.
02:56
The mode of the object that .split()
returns is 'L'
, indicating this is a grayscale image or an image that only displays the luminance values of each pixel.
03:06
Now you can create three new RGB images showing the red, green, and blue channels separately using merge()
, which is a function in the Image
module. To create a band containing zeros everywhere, you use the .point()
method.
03:22
.point()
needs a function as an argument. The function that you use determines how each point transforms. In this case, you use a lambda function to map each point to 0
to create the image showing only the red channel.
03:35
You merge the red band from the original image with the green and blue bands that contain only zeros. The red band alone, stored in the variable red
, is a grayscale image with mode L.
03:51
The first argument in merge()
determines the mode of the image that you want to create. The second argument contains the individual bands that you want to merge into a single image.
04:01
So here you use the red channel from the original image and two bands of zeros for the green and blue channels. When you merge the red band with green and blue bands containing zeros, you get an RGB image called red_merge
. Therefore, the RGB image that you create only has non-zero values in the red channel, but because it’s an RGB image, it will display in color.
04:26
You repeat a similar process to obtain green_merge
and blue_merge
, which contain RGB images with the green and blue channels, respectively.
04:51 On-screen you can see the three images generated by the code. The red image contains a strong signal in the pixels that represent the strawberry because these pixels are mostly red.
05:03 The green and blue channels show these pixels as dark because they have small values. The exceptions are those pixels that represent the reflection of the light on the surface of the strawberry as these pixels are nearly white.
05:16 Now that you have some experience with Pillow under your belt, in the next section of the course, you’ll start processing images using the library.
Become a Member to join the conversation.
eyrei RP Team on Aug. 15, 2023
That was a really interesting tutorial, Darren. Like most folk I’ve used editing tools to achieve these effects, but this gives a taster of how they work.