Greyscale Images and More

In this lesson, we'll learn about grayscale images, pixel colors, and other image formats, in this lesson.

Grayscale images

Some images do not support color, and they only allow for shades of gray to be used. These are called grayscale images, or monochrome images. They are similar to old-style black and white films or photographs. These days they are used for artistic effect, or to contain images that have no color content.

Pycairo doesn’t directly support grayscale output. We can create objects in gray by setting the red, green, and blue values to be equal. For example, 1.0 is white, 0.5 is mid-grey, 0.0 is black.

When converting an RGB image into a grayscale image, a general rule of thumb is to take the average of the three RGB colors and use that average for all three RGB colors. For example, let’s say we have a red rectangle: (r = 1, g = 0, b = 0), we will take the average, (1+0+0)/3=0.33(1+0+0)/3 = 0.33, and use it like this: (r = 0.33, g = 0.33, b = 0.33)

Similarly, if we had a rectangle with the RGB values (r = 1, g = 0, b = 1), we would use it like this: (r = 0.66, g = 0.66, b = 0.66).

Check the playground below to see grayscale rectangles:

Get hands-on with 1200+ tech skills courses.