How to create an image pyramid using Pillow?

Jan 12, 2026

Leave a message

Noah Davis
Noah Davis
Noah is a product designer at Yangzhou Xinkai Textile Hotel Supplies Factory. He combines fashion and practicality, creating unique hotel supplies that are popular among many hotels and resorts.

Creating an image pyramid is a useful technique in image processing that allows you to represent an image at multiple scales. In this blog post, I'll guide you through the process of creating an image pyramid using the Pillow library, and also highlight our offerings as a Pillow supplier.

What is an Image Pyramid?

An image pyramid is a collection of images - all derived from a single original image - that are successively downsampled until some desired stopping point is reached. There are two common types of image pyramids: Gaussian and Laplacian. Gaussian pyramids are used for downsampling images, while Laplacian pyramids are used for reconstructing an upsampled image from an image lower in the pyramid.

Prerequisites

Before we start, make sure you have the Pillow library installed. If not, you can install it using pip:

pip install pillow

Creating a Gaussian Image Pyramid

Let's start by creating a Gaussian image pyramid. The following Python code demonstrates how to do this:

from PIL import Image

def gaussian_pyramid(image, levels):
    pyramid = [image]
    for i in range(levels - 1):
        image = image.resize((image.width // 2, image.height // 2), Image.BICUBIC)
        pyramid.append(image)
    return pyramid

# Open an image
original_image = Image.open('your_image.jpg')

# Generate a Gaussian pyramid with 4 levels
pyramid = gaussian_pyramid(original_image, 4)

# Save each level of the pyramid
for i, level in enumerate(pyramid):
    level.save(f'gaussian_level_{i}.jpg')

In this code, we define a function gaussian_pyramid that takes an image and the number of levels as input. It then successively downsamples the image by a factor of 2 using the resize method with the Image.BICUBIC resampling filter. The resulting images are stored in a list, which represents the Gaussian pyramid.

Creating a Laplacian Image Pyramid

To create a Laplacian image pyramid, we first need to create a Gaussian pyramid. Then, we can compute the Laplacian images by subtracting the upsampled version of each level in the Gaussian pyramid from the next level. Here's the code:

from PIL import Image

def gaussian_pyramid(image, levels):
    pyramid = [image]
    for i in range(levels - 1):
        image = image.resize((image.width // 2, image.height // 2), Image.BICUBIC)
        pyramid.append(image)
    return pyramid

def laplacian_pyramid(gaussian_pyramid):
    levels = len(gaussian_pyramid)
    laplacian = []
    for i in range(levels - 1):
        upsampled = gaussian_pyramid[i + 1].resize((gaussian_pyramid[i].width, gaussian_pyramid[i].height), Image.BICUBIC)
        diff = ImageChops.difference(gaussian_pyramid[i], upsampled)
        laplacian.append(diff)
    laplacian.append(gaussian_pyramid[-1])
    return laplacian

# Open an image
original_image = Image.open('your_image.jpg')

# Generate a Gaussian pyramid with 4 levels
gaussian = gaussian_pyramid(original_image, 4)

# Generate a Laplacian pyramid
laplacian = laplacian_pyramid(gaussian)

# Save each level of the Laplacian pyramid
for i, level in enumerate(laplacian):
    level.save(f'laplacian_level_{i}.jpg')

In this code, we first define the gaussian_pyramid function as before. Then, we define a new function laplacian_pyramid that takes a Gaussian pyramid as input. It computes the Laplacian images by subtracting the upsampled version of each level in the Gaussian pyramid from the next level using the ImageChops.difference method.

Our Pillow Products

As a pillow supplier, we offer a wide range of high-quality pillows to meet your needs. Whether you're looking for pillows for a High-star Hotel Pillows, or Organic Health Buckwheat Pillow, or Goose Down Pillows, we have you covered.

Our pillows are made from the finest materials and are designed to provide maximum comfort and support. We understand the importance of a good night's sleep, and our pillows are engineered to help you achieve just that.

Conclusion

Creating an image pyramid using the Pillow library is a straightforward process that can be useful in a variety of image processing applications. Whether you're working on computer vision, image compression, or simply want to explore different ways of representing an image, image pyramids are a powerful tool.

If you're interested in our pillow products, we invite you to contact us for a procurement discussion. We're committed to providing the best products and services to our customers, and we look forward to working with you.

Organic Health Buckwheat Pillow high qualityOrganic Health Buckwheat Pillow

References

  • Pillow documentation: https://pillow.readthedocs.io/en/stable/
  • Image processing concepts: Digital Image Processing by Rafael C. Gonzalez and Richard E. Woods
Send Inquiry
Contact usif have any question
You can either contact us via phone, email or online form below. Our specialist will contact you back shortly.
contact us