Get the Basic image information with Pillow | Python

In this tutorial, we are going to learn how to get the basic information about an image using Python Imaging Library(PIL) i.e also known as ‘Pillow’. In this library, there are many modules but we will be using only the ‘Image’ module because it contains all the functions that are required for retrieving the information from an image.

While working in the Image Processing domain more often you need to work with image data, therefore it is essential to know how to retrieve the required information from an input image, and even though you not doing Image Processing it always good to know something about a field instead of nothing.

Image Module Functions in Python

There are many functions that are supported by the Image Module for Image Processing, it is not possible to discuss each and every function in detail here. So, we will be discussing only the required functions that we are going to use afterward in our code.

  • Image.filename – This function is used to get the file name or the path of the image. If the image is not opened using ‘open()’ function it returns the null string.
  • Image.format – This function is used to get the file format of the image like JPEG/JPG, PNG, GIF, etc.
  • Image.mode – This function is used to get the pixel format of the image like RGB, RGBA, CMYK, etc.
  • Image.size – This function returns the tuple consist of width & height of the image.
  • Image.width –  This function returns only the width of the image.
  • Image.height – This function returns only the height of the image.
  • Image.palette – This function returns the color palette table if any, otherwise returns none.
  • Image.info – This function returns some non-image information in dictionary format.

To learn more about Image Module or Pillow library you can refer to its documentation.

Image information in Python with Pillow

First of all, we need to install the required libraries or modules:

Installation:

pip3 install pillow

Now comes the actual code, I would suggest you read the code it is very sweet, short and simple code to understand. Afterward, I’ll be explaining to you the code thoroughly.

Source Code:

# Importing Required Library
from PIL import Image

# Opening Image as an object
Img = Image.open("test.jpg")
# Getting the filename of image
print("Filename : ",Img.filename)
# Getting the format of image
print("Format : ",Img.format)
# Getting the mode of image
print("Mode : ",Img.mode)
# Getting the size of image
print("Size : ",Img.size)
# Getting only the width of image
print("Width : ",Img.width)
# Getting only the height of image
print("Height : ",Img.height)
# Getting the color palette of image
print("Image Palette : ",Img.palette)
# Getting the info about image
print("Image Info : ",Img.info)
# Closing Image object
Img.close()

After reading the code you would have come to know two things that the image used is named as ‘test’ and it is in ‘JPEG/JPG’ format. So, the answer to the first two lines has been already decoded. Next comes the mode, as it is a JPG image so its mode would probably be ‘RGB’ if it would be PNG/GIF or any other format, then it could be ‘RGBA/RGB/CYMK’. Next comes the size of the image, so it would return the tuple consisting of the width and height of Image in ‘Pixels’. If we want width and height separately we can use the other two functions below. Next comes the image color palette if it would exist the table would return else None. Next comes the image info, it would return some ‘JFIF’ information related to image like its version, unit, density, etc. Let’s see what all comes out of it.

Output:

Image Used: Image information in Python with Pillow
Information Retrieved:

Image information in Python with Pillow

So In this way, we can get the basic information of the image using python, I hope this tutorial was helpful to you. Thank you ‘Keep learning Keep Coding’.

Leave a Reply

Your email address will not be published.