Colour spaces in Pillow


Martin McBride, 2020-09-20
Tags colour, rgb, transparency, greyscale
Categories pillow
In section Python libraries



As we saw in the previous section, colour is essentially a 3 dimensional quantity. It can be represented by 3 values, red, green and blue. Each unique combination of red, green and blue creates a unique colour.

You could think of these values as axes in a 3 dimensional space, where each point in space is a particular colour. We call this space the RGB colour space.

There are other ways to represent colours. They typically use 3 values, but rather than red, green and blue they use a different set of values (we call this a different colour space). Pillow supports several types of colour space:

  • CMY colour spaces are used in printing, where cyan, magenta and yellow inks are used to create colours. Pillow supports CMYK, which adds a fourth ink, black, mainly used for printing text.
  • HSL (hue, saturation, lightness) colour spaces represents a colour as a hue (that represents the basic, underlying colour), saturation (that represents how pure the colour is), and lightness (how light or dark the colour is). Pillow supports HSL and the similar HSB system.
  • Luminance/chrominance colour spaces represents colours using a brightness component, and two other values that represent chromatic (colour component). Pillow supports YCbCr and L*a*b.
  • Greyscale images only contain shades of grey, with no colour (like black and white photographs).

Pillow also supports:

  • Named colours, where a particular colour is specified by its name
  • Palette based images, where each pixel contains an index into a colour table.

RGB colours

In RGB space, colour is represented by 3 numbers that specify the amount of red, green and blue that make up the colour. In this section will use percentages, where 0% means none of that colour is present, 100% means the maximum amount of that colour is present. So for example:

rgb(100%, 50%, 0%)

Indicates a colour that contains the maximum amount of red. 50% of the maximum green, and no blue. That would give an orange colour.

RGB is a natural way to represent colours on a computer, because it models how a computer screen works. As we saw previously, the eye senses colour by the amount of red, green and blue light present. This means that we can simulate almost any visible colour by mixing the correct amount of red, green and blue light. In a computer screen, each pixel has three tiny elements (typically light-emitting diodes) than acts as a variable source of red, green and blue light. By illuminating these sources by the correct amount, we can set each pixel to any colour.

We call RGB an additive model, because it works by adding separate red, green and blue lights sources.

Here are a selection of example colours:

This table shows every combination of red, green and blue values of 0%, 50% and 100%.

CMYK colours

When we print an image, we normally apply different coloured inks to white paper to create the colours we require.

A printed page works quite differently to a computer screen. A screen starts is dark by default, and adds red, green and blue light to create colours. A printed page doesn't generate light, it simply reflects light. A blank page starts off white (assuming it is white paper viewed under white light), and the ink on the page absorbs different amounts of red, green and blue light, to leave the required colour. We call this a subtractive model.

Many printing processes work by adding several layers of different coloured translucent inks. Here is an illustration:

Because the ink is translucent, light passes through the ink, and reflects off the white paper underneath. The ink acts as a colour filter, removing certain colours.

We normally use cyan, magenta and yellow ink. Cyan ink filters out red light, leaving just green and blue (cyan is a mixture of green and blue, so when we remove the red from white light, we are left with cyan light). Magenta ink filters out green light (magenta is a mixture of red and blue). Yellow ink filters out blue light (yellow is a mixture of red and green).

If we put one layer of ink on top of another, light passes through both layers and reflects off the page. For example:

In this case, magenta ink has been layered over cyan ink. The cyan ink removes the red light, the magenta ink removes the green light, so we end up with a blue colour in the page. In fact, by layering different amounts of cyan, magenta and yellow ink on the page, it is possible to remove different amounts of red, green and blue from the reflected light, which allows you to print any colour.

The K component

If you own a colour inkjet printer or similar, you will probably know that it has 4 colours, cyan, magenta, yellow and black. The K part of CMYK stands for Key, which is an old printing term for the black plate in a traditional printing press.

In theory, we shouldn't need a separate black ink, because mixing cyan, magenta and yellow ink together should create black. But in reality, there are several advantages to using black ink:

  • Equal quantities of cyan, magenta an yellow ink don't actually create a perfect black. They usually create a very dark brown.
  • In most print processes, the three colours don't exactly align on the page. This effect is most pronounced if you print small black features (such as text), and makes the feature appear blurred or fuzzy.

Many colour printing processes - from documents that people might print on a home or office printer, through to newspapers or magazines - often feature significant amounts of black text. CMY printing generally doesn't create black text of suitable quality, so the best solution is usually to add separate black colour specifically for text and other 100% black features.

Black ink is also considerably cheaper to produce than coloured ink.

Greyscale colours

Any RGB colour that has equal amounts of red, green and blue, will display as a shade of grey, for example:

  • rgb(0%, 0%, 0%) is black.
  • rgb(30%, 30%, 30%) is dark grey.
  • rgb(100%, 100%, 100%) is white.

In the greyscale colours space, the colour is specified by a single value. The colour is displayed as if the red, green and blue values were all equal. You can think of greyscale as a subset of RGB that includes only the pure grey colours.

The greyscale colour space can only show shades of grey. It is useful for images that don't include any colour, for example:

  • Text only documents.
  • Charts and diagrams that don't use colour.
  • Scans of black and white photographic images.

This image

A greyscale image only stores 1 value per pixel (the grey level), whereas an RGB image store 3 values per pixel.

1-bit images

1-bit images are an extreme example of a greyscale colour space. They only allow each pixel to be either black or white, so a pixel value can be stored in a single bit (that is, 8 pixels can be stored in 1 byte). These are sometimes used for images that contain only black text and black line drawings, so there are no grey values.

HSL Colours

The HSL colour space (and the closely related HSV space) store colours as 3 values:

  • The Hue (H) indicates the basic colour, essentially the position of the colour on the colour wheel that goes from red to green to blue then back to red.
  • The Saturation (S) controls how saturated the colour is. A value of 100% indicates a pure colour, 50% represents the same colour but mixed with grey, and at 0% the colour is pure grey.
  • The Lightness (L) controls how light or dark the colour is. Lightness of 50% shows the basic colour at a medium level of lightness. If the L component increases towards 100%, the colour gets lighter and lighter until it eventually becomes white. If the L component decreases towards 0%. the colour gets darker and darker until it eventually becomes black.

This diagram shows the colour wheel of hue values. The hue can be expressed as an angle between 0 and 360 degrees, where 0 degrees is red, 120 degrees is green, and 240 degrees is blue:

Here is the effect of changing the saturation between 0% and 100% (with a hue of green and lightness of 50%). Notice that the basic colour remains the same:

Finally, here is the effect of changing the lightness between 0% and 100% (with a hue of green and saturation of 50%). Again, the basic colour remains the same:

HSL colours are useful in art and design applications, because they allow sets of related colours to be created very easily.

HSL variants

The HSB (hue, saturation, brightness) colour space uses the same definition of hue as HSL. Saturation and brightness behave differently, but can achieve the same range of colours as HSL.

The HSL model works well with the additive model (as used by RGB). HSB is based on a subtractive colour model, but generally CMYK is more useful for subtractive colours. HSB tends not to be used as often as HSL, so we will not cover it in detail.

You may also see the term HSV (hue, saturation, value). This is an alternative name for HSB.

Luminance chrominance colour spaces

Luminance chrominance colour spaces consist of 3 components:

  • Luminance contains the overall brightness of the image. It is a bit like a greyscale image - if you looked at just the luminance component it would look similar to a black and white photograph.
  • Two chrominance components. These indicate the colour once the lightness component has been removed.

There are several colour spaces that work in this way. Pillow supports YCbCr and L*a*b.

The two chrominance components contain similar information to the hue and saturation components of HSL, but it is coded differently. For example, in L*a*b, the *a component represents the position of the colour between magenta and green, the *b component represents the position of the colour between yellow and blue.

Luminance chrominance colour spaces have several uses:

  • Analogue television. Black and white television was first broadcast in the mid 1930s, but it was another 30 years before colour TV was introduced. To allow for both systems, colour TV was broadcast as luminance (the original B&W signal) and a separate chrominance signal containing the colour information (that B&W sets would not pick up).
  • Image compression. The eye can detect luminance information at greater resolution than chrominance (due to there being more rod cells than cone sells in the retina). JPEG image compression, and some video compression systems, take advantage of that fact. They encode the image data in a luminance chrominance space, but subsample the chrominance data.
  • CIELAB is a widely used device independent colour space that uses luminance and chrominance to accurately represent colours as they are perceived by the human eye.

If you found this article useful, you might be interested in the book Computer Graphics in Python, or other books, by the same author.

<<Prev

Tag cloud

2d arrays abstract data type alignment and array arrays bezier curve built-in function close closure colour comparison operator comprehension context conversion data types design pattern device space dictionary duck typing efficiency encryption enumerate filter font font style for loop function function composition function plot functools generator gif gradient greyscale higher order function html image processing imagesurface immutable object index inner function input installing iter iterator itertools lambda function len linspace list list comprehension logical operator lru_cache mandelbrot map monad mutability named parameter numeric python numpy object open operator optional parameter or partial application path positional parameter print pure function radial gradient range recipes recursion reduce rgb rotation scaling sequence slice slicing sound spirograph str stream string subpath symmetric encryption template text text metrics transform translation transparency tuple unpacking user space vectorisation webserver website while loop zip

Copyright (c) Axlesoft Ltd 2020