Extracting Image Attributes Using Pillow

  • There are many basic attributes of an Image frequently used in the internal decision making of the image-processing routines.

 

  • Pillow - the Python Image Processing Library provides Image class, which represents a generic image. Any of the several image types supported by Pillow, can be loaded with the Image class.

 

  • Image Format: The format attribute of the Image class describes what format the loaded images are. It could be JPEG, PNG, GIF or any other supported format.

 

  • Image Mode: The mode describes what kind of pixel format is used in an image.

e.g., If the mode is "L" the pixels are in grey scale.

If the mode is "RGB" the image uses RGB color model.

If the mode is "RGBA" the image uses RGBA color model where the letter “A” represents the alpha channel.

 

  • Image Size: Any 2-D image is a matrix of pixels. To process an image, it is often required to navigate through the 2D-pixel buffer and perform the required image processing operations pixel by pixel. The boundary condition for any such loop is based on the size of the image - which defines the width and height of the image.

 

  • The image class has the size attribute, which is a tuple of width and height.

 

  • Palette: Many a times it is required to represent an image with fewer colors than the supported one byte per one basic color scheme. The basic colors are Red, Green and Blue. Having fewer colors reduces the memory footprint, reduces network and IO overhead and improves application performance in several cases like Gaming and others.

 

  • If an image with RGB color model can represent 256 types of red, 256 types of green, 256 types of blue using one byte per color model that is 3 bytes per pixel - an Image with a palette of 8 colors uses only 3 bits of a byte or the whole byte (leaving 5 bits unused) still saving 2 bytes of storage per pixel.

 

  • If an image has a mode "P" it means it has an associated color palette and can be retrieved using the palette attribute of the Image object.

 

  • Additional Image Information: Any other information belonging to a specific type of Image can be added using the info attribute, which is a python dictionary, which can store information as key, value pairs.

 

  Example:

from PIL import Image

 

# Define a method for printing image attributes

def printImageAttributes(imageObject, imagePath):

    # Retrieve the attributes of the image

    fileFormat      = imageObject.format        # Format of the image

    imageMode       = imageObject.mode          # Mode of the image

    imageSize       = imageObject.size          # Size of the image - tupe of (width, height)

    colorPalette    = imageObject.palette       # Palette used in the image

 

 

    # Print the attributes of the image

    print("Attributes of image:%s"%imagePath)

 

    print("The file format of the image is:%s"%fileFormat)

    print("The mode of the image is:%s"%imageMode)

    print("The size of the image is:width %d pixels,height %d pixels"%imageSize)

    print("Color palette used in image:%s"%colorPalette)

 

    print("Keys from image.info dictionary:%s")

    for key, value in imageObject.info.items() :

        print(key)

 

# Create an image object and show the image

imagePath       = "./colors.jpg"

imagePathTemp   = "TempImage"

 

image           = Image.open(imagePath)

 

image.show()

 

# Create an image object with color palette and show the image

imageWithColorPalette = image.convert("P", palette=Image.ADAPTIVE, colors=8)

imageWithColorPalette.show()

 

# Print the attributes of the images

printImageAttributes(image, imagePath)

print("==================")

printImageAttributes(imageWithColorPalette, imagePathTemp)

 

Output:

Image 1:

Image 1 - Example for extracting image attributes using Python - Pillow

Image 2:

Image 2 - Example for extracting image attributes from an image with color palette - Pillow

Attributes of image:./colors.jpg

The file format of the image is:JPEG

The mode of the image is:RGB

The size of the image is:width 640 pixels,height 427 pixels

Color palette used in image:None

Keys from image.info dictionary:%s

jfif

jfif_version

dpi

jfif_unit

jfif_density

icc_profile

==================

Attributes of image:TempImage

The file format of the image is:None

The mode of the image is:P

The size of the image is:width 640 pixels,height 427 pixels

Color palette used in image:<PIL.ImagePalette.ImagePalette object at 0x101600f28>

Keys from image.info dictionary:%s

jfif

jfif_version

dpi

jfif_unit

jfif_density

icc_profile

 


Copyright 2018 © pythontic.com