How to Obtain Attributes of an Image in Python using the Pillow Module



Python


In this article, we show how to obtain attributes of an image in Python using the Pillow module.

So let's say that you have an automated website, for example, a site that has members and each member can upload pictures, including a picture such as a profile picture.

In certain cases, you may need to obtain or extract attributes of the image, such as the size, the height, the width, the filename, the format (jpg, png, etc.), etc.

So how can we extract all of these attributes from an image in Python using the Pillow module?

We will show all of this below.

Let's say that a user uploaded the following image below.



Let's say the user was shy and didn't want to put their actual picture up but a picture of a flower.

In the code below, we extract all the attributes of this image.



Let's now go over this code.

First, we have to import Image from the pillow module.

We then create a variable, image1, which will store the image we are working with in this example. In this case, it is 'Flower.jpg'.

The first attribute that we will obtain from the image is the size attribute. This gets the width and height attribute of the image.

We then further separate the width and height attributes by using a tuple, and setting it equal to, image1.size

We can then get the filename of the variable, image1, through the filename attribute. This gives us the name of the file and the type of file that it is (jpg, png, etc.)

Next, we get the type of file it is through the format attribute. In this case, this image is a png file format.

The last attribute we get is a description of the file format through the format_description attribute. This is like the format attribute, but instead of just giving what the file format is, it gives a description. For this png image, it specifies what png stands for, which is Portable network graphics.

So again, we can obtain attributes from an image that includes things, such as the size, filename, and format of the image.

So these are ways we can obtain attributes of images in Python using the Pillow module.


Related Resources

How to Draw a Rectangle in Python using OpenCV

How to Draw a Circle in Python using OpenCV

How to Draw a Line in Python using OpenCV

How to Add Text to an Image in Python using OpenCV

How to Display an OpenCV image in Python with Matplotlib

How to Use Callback functions to Connect Images to Events in Python using OpenCV

How to Check for Multiple Events in Python using OpenCV



HTML Comment Box is loading comments...