Last modified: Apr 21, 2025 By Alexander Williams

Python Simple Image Grids Guide

Creating image grids in Python is a useful skill. It helps organize multiple images into a single layout. This guide will show you how.

Why Use Image Grids?

Image grids make visuals cleaner. They are great for comparisons or presentations. Python makes grid creation easy.

Required Libraries

You need Pillow (PIL) and NumPy. Install them using pip.


pip install pillow numpy

Basic Image Grid Example

Here's a simple way to create a 2x2 image grid. We use the PIL.Image module.


from PIL import Image

# Load images
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg")
img3 = Image.open("image3.jpg")
img4 = Image.open("image4.jpg")

# Create a new blank image
grid = Image.new('RGB', (img1.width * 2, img1.height * 2))

# Paste images into grid
grid.paste(img1, (0, 0))
grid.paste(img2, (img1.width, 0))
grid.paste(img3, (0, img1.height))
grid.paste(img4, (img1.width, img1.height))

# Save the grid
grid.save("image_grid.jpg")

This code combines four images into one grid. Adjust sizes as needed.

Using OpenCV for Image Grids

OpenCV offers more control. It's great for advanced image analysis.


import cv2
import numpy as np

# Load images
img1 = cv2.imread("image1.jpg")
img2 = cv2.imread("image2.jpg")
img3 = cv2.imread("image3.jpg")
img4 = cv2.imread("image4.jpg")

# Combine horizontally
top = np.hstack((img1, img2))
bottom = np.hstack((img3, img4))

# Combine vertically
grid = np.vstack((top, bottom))

# Save the grid
cv2.imwrite("opencv_grid.jpg", grid)

Note: All images must have the same dimensions for this to work.

Adding Borders Between Images

Borders improve grid readability. Here's how to add them.


from PIL import Image, ImageOps

# Load images (same as before)
images = [img1, img2, img3, img4]

# Add borders
bordered = [ImageOps.expand(img, border=10, fill='white') for img in images]

# Create grid (same as before)
grid = Image.new('RGB', (bordered[0].width * 2, bordered[0].height * 2))
grid.paste(bordered[0], (0, 0))
grid.paste(bordered[1], (bordered[0].width, 0))
grid.paste(bordered[2], (0, bordered[0].height))
grid.paste(bordered[3], (bordered[0].width, bordered[0].height))

grid.save("bordered_grid.jpg")

Dynamic Grid Creation

For variable numbers of images, use this dynamic approach. It's useful for image classification results.


import math
from PIL import Image

def create_grid(images, cols):
    rows = math.ceil(len(images) / cols)
    width = images[0].width
    height = images[0].height
    
    grid = Image.new('RGB', (width * cols, height * rows))
    
    for i, img in enumerate(images):
        grid.paste(img, ((i % cols) * width, (i // cols) * height))
    
    return grid

# Usage
images = [Image.open(f"image{i}.jpg") for i in range(1, 7)]
grid = create_grid(images, 3)
grid.save("dynamic_grid.jpg")

Conclusion

Creating image grids in Python is simple with PIL or OpenCV. These techniques help in image collages and presentations.

Experiment with different layouts and borders. Combine this with other techniques like image cropping for better results.