Plotting a grid of PIL images in Jupyter

When working with images in a Python notebook I like to visualize them on a grid. Just calling display is not enough as it renders the images underneath each other. Let's use Matplotlib to generate a grid of PIL images.

Download an image into PIL

First, let's create a helper to download images and convert them to PIL images. You'll probably won't need this, but it shows that PIL images are used (instead of files). To make things easier, I'm adding the last part of the URL path as a filename.

import io
from urllib.request import urlopen, Request
from urllib.parse import urlparse
from PIL import Image
from PIL.Image import Image as PilImage

def url_to_pil(url:str) -> PilImage:
    headers = { 'User-Agent': 'Mozilla/6.0'}
    data = urlopen(Request(url, headers=headers)).read()
    bts = io.BytesIO(data)
    im =
    im.filename = urlparse(url).path
    return im

We can now map a bunch or URLs to PIL images like this:

urls = [

images = [url_to_pil(u) for u in urls]

Display images helper

Let's create a helper function that will plot the images:

import matplotlib.pyplot as plt
from PIL.Image import Image as PilImage
import textwrap

def display_images(
    images: [PilImage], 
    columns=5, width=20, height=8, max_images=15, 
    label_wrap_length=50, label_font_size=8):

    if not images:
        print("No images to display.")

    if len(images) > max_images:
        print(f"Showing {max_images} images of {len(images)}:")

    height = max(height, int(len(images)/columns) * height)
    plt.figure(figsize=(width, height))
    for i, image in enumerate(images):

        plt.subplot(len(images) / columns + 1, columns, i + 1)

        if hasattr(image, 'filename'):
            if title.endswith("/"): title = title[0:-1]
            title=textwrap.wrap(title, label_wrap_length)
            plt.title(title, fontsize=label_font_size); 

The width and height are in inches, so you might want to specify different values based on your need. Now let's call the helper for our image array:


Which results in the following plot image:

Showing a grid of images with some nice labels.

How sweet is that?


2020-05-08: Fixed bug with displaying a list of images that is less than halve the number of columns. Now making sure there is at least 1 row rendered if we have images.
2020-05-07: Added labels with the filename to the grid. This makes it easier to distinguish between images. Added option for wrapping long image names. This is especially useful if file names are long.