Pillow Image Attributes

In this chapter, you will learn about the various attributes of the Pillow Image object. You will also learn how to convert images from one mode to another.

format attribute

The format attribute of an Image class object stores the format of the file from which the image was read.

Pillow library supports many formats and almost all commonly used ones such as JPEG, PNG, BMP, PPM, ICO and GIF among many others.

Example

print(img.format)
# Outputs- 'PNG'

height attribute

The height attribute of an Image class object stores the height of the image.

Example

print(img.height)
# Outputs- 2448

width attribute

The width attribute of an Image class object stores the width of the image.

Example

print(img.width)
# Outputs- 3264

size attribute

The size attribute of an Image class object stores a tuple that contains the size(the height and the width) of the image as (width, height).

Example

print(img.size)
# Outputs- (3264, 2448)

mode attribute

The mode attribute of an Image class object stores information about the number and names of bands in an image.

The Pillow library supports various modes and also supports conversion between them. The table below contains the most commonly used mode along with their brief descriptions.

ModeDescription
LEach pixel consists of 1 number ranging from 0-255(both inclusive). It represents a grayscale image.
RGBEach pixel consists of 3 numbers ranging from 0-255(both inclusive). These numbers denote the of Red, Green and Blue.
RGBAEach pixel consists of 4 numbers ranging from 0-255(both inclusive). These numbers denote the brightness of Red, Green and Blue along with alpha(transparency).
HSVEach pixel consists of 3 numbers ranging from 0-255(both inclusive). These numbers denote the Hue, Saturation and Value.
CMYKEach pixel consists of 4 numbers ranging from 0-255(both inclusive). These numbers denote the brightness of Cyan, Magenta, Yellow and Key(Black).

Example

print(img.mode)
# Outputs- 'RGB'

convert()

Pillow’s convert() method is used to change the colour mode of an image. Sometimes this may result in a change in the colour of the image.

Syntax:

image_object.convert(mode)

Example

In this example, we will be converting a colour image(of the mode RGB) into a black and white image(of the mode L).

img_bw = img.convert('L')

img_bw.show()

The output of this will be-

Pillow Image Attributes. An image converted to black and white using convert

While converting a black and white image to a colour one the mode of the image is changed, however, the image will be identical to what it was before. This is because once an image is converted from colour to a black and white image, information about its colour is destroyed. This information cannot be recovered during its conversion back to a colour image.

Example

In this example, we will be taking a black and white image(of the mode L) and try to convert it into a colour image(of the mode RGB).

img_restored = img_bw.convert('RGB')

print(img_restored.mode)
# Outputs- 'RGB'
# Notice that the Mode of the image changes

img_restored.show()

The output of this will be-

A black and white image cannot be convert to a color image.

Note– Converting a black and white image to a colour image will change its mode but not its actual colour.

Example

In this example, we will be converting a color image(of mode RGB) into another color image(of mode CMYK).

img_cmyk = img.convert('CMYK')

print(img_cmyk.mode)
# Outputs- 'CMYK'

img_cmyk.show()

The output of this will be-