Python Program to Find the Size (Resolution) of a Image

Updated on December 30, 2024
Find the size (resolution) of a image header image

Introduction

Determining the size or resolution of an image is a fundamental task in many image processing applications. Being able to extract these attributes means you can dynamically modify, display, or analyze image data based on its dimensions. Python, equipped with powerful libraries like PIL (Pillow), makes this task straightforward.

In this article, you will learn how to find the size or resolution of an image using Python. Explore how to use the Pillow library to accomplish this with clarity and ease. Delve into practical examples that demonstrate reading and manipulating image properties.

Finding Image Resolution with Pillow

Pillow is the friendly PIL fork and an efficient library for opening, manipulating, and saving many different image file formats in Python. Here, focus on obtaining the resolution of images in various formats.

Install Pillow Library

Before diving into the code, ensure that the Pillow library is installed on your system. Here's how to install it using pip:

console
pip install Pillow

Basic Example to Get Image Size

  1. Import the Image module from the PIL library.

  2. Open an image using Image.open().

  3. Retrieve image size using the size attribute.

    python
    from PIL import Image
    
    # Load an image
    image = Image.open('path/to/your/image.jpg')
    
    # Get the size of the image
    width, height = image.size
    
    print(f"The image resolution is: {width}x{height}")
    

    This code snippet opens an image and uses the size attribute, which returns a tuple containing the width and height of the image. This tuple is then unpacked into width and height, which are printed in the console.

Dealing with Multiple Files

To handle multiple images, iteratively process each image inside a directory and retrieve their sizes.

  1. Import necessary modules.

  2. Define a directory containing images.

  3. Loop through each file, load the image, and print its size.

    python
    import os
    from PIL import Image
    
    # Directory containing images
    dir_path = 'path/to/your/directory'
    
    # List all files in the directory
    for file_name in os.listdir(dir_path):
        # Construct full file path
        file_path = os.path.join(dir_path, file_name)
        # Open and find the image size
        with Image.open(file_path) as img:
            print(f"{file_name}: {img.size}")
    

    This code lists all files in a directory and processes each one through the Image.open() method. It retrieves the size of each image and prints the filename alongside its resolution.

Handling Images from URLs

In some cases, you might want to retrieve the size of an image hosted online without downloading it entirely.

  1. Import necessary modules, including requests.

  2. Use requests.get() to stream the image.

  3. Open the image directly from bytes.

    python
    import requests
    from PIL import Image
    from io import BytesIO
    
    # URL of the image
    url = 'https://example.com/path/to/image.jpg'
    
    # Fetch the image
    response = requests.get(url, stream=True)
    response.raw.decode_content = True
    
    # Open the image
    with Image.open(response.raw) as img:
        print(f"Online image size: {img.size}")
    

    This snippet fetches an image from a URL using requests with streaming enabled to avoid downloading the entire image. The image is then opened directly from the raw data stream using Image.open().

Conclusion

Extracting the size or resolution of images using Python and Pillow is both efficient and easy to encapsulate within functional parts of programs. Whether dealing with local files, multiple images, or online sources, Pillow provides a consistent and straightforward interface for image manipulation. Apply the concepts learned here to enhance applications that rely on dynamic image resizing, checks, or processing. By mastering these techniques, maintain clean, readable, and efficient Python code that effectively manages image data.