PythonIntermediate · Windows, macOS, or Linux with Python 3.12+

Build a labeled image contact sheet

Arrange up to 100 images into a labeled overview grid.

Before you begin

  • Python 3.12 or newer
  • Pillow installed in the same Python environment
What this script changesCreates new image files or a new frame directory. Inputs and outputs are limited to 20 million pixels per image; animated tasks also cap frame count. Ordinary transforms process the first frame and normalize EXIF orientation. Color profiles and original metadata are not preserved reliably; use source copies for archival work.

Set up your example

  1. Save the complete code as image-contact-sheet.py in a working folder.
  2. Save the provided sample files in that same folder; run commands from that folder.
  3. Install Pillow: python -m pip install Pillow
  4. Generate the original fixtures once: python "make_samples.py". Choose an empty folder so the fixture files do not exist.

Sample files in this download:

  • make_samples.py

Download the ZIP, extract it, and open a terminal inside the extracted folder. The source file is image-contact-sheet.py.

Run the script

Run command
python "image-contact-sheet.py" --input "sample.png" --extra "second.png" --columns 2 --cell 180 --output "sheet.png"

If your system uses python3 instead of python, use that command for Python examples. Keep paths containing spaces in quotes. Required inputs and optional settings are described in the code and setup steps.

What to expect

Arrange up to 100 images into a labeled overview grid. The example makes a 360 by 204 sheet containing the two fixtures.

The complete script

image-contact-sheet.py
#!/usr/bin/env python3
'Build a labeled image contact sheet'
import argparse
import json
from pathlib import Path

def write_json(path, value):
    data = json.dumps(value, indent=2, ensure_ascii=False, allow_nan=False) + "\n"
    with Path(path).open("x", encoding="utf-8", newline="") as handle:
        handle.write(data)

import math

try:
    from PIL import Image, ImageOps, ImageEnhance, ImageFilter, ImageDraw, ImageFont, ImageChops
except ImportError:
    raise SystemExit("Install Pillow first: python -m pip install Pillow")
MAX_PIXELS=20_000_000
def check_size(size):
    if min(size)<1 or size[0]*size[1]>MAX_PIXELS:
        raise ValueError("Image dimensions must be positive and at most 20 million pixels.")
def read_image(path):
    with Image.open(path) as original:
        check_size(original.size)
        return ImageOps.exif_transpose(original).convert("RGBA")
def save_image(path,image,format="PNG"):
    check_size(image.size)
    with Path(path).open("xb") as handle:
        image.save(handle,format=format)

def main():
    parser = argparse.ArgumentParser(description='Build a labeled image contact sheet')
    parser.add_argument("--input",required=True,help="Local source image")
    parser.add_argument("--output",required=True,help="New output file or directory")
    parser.add_argument("--extra",nargs="+",required=True)
    parser.add_argument("--columns",type=int,default=3)
    parser.add_argument("--cell",type=int,default=180)
    args = parser.parse_args()
    paths=[args.input]+args.extra
    if len(paths)>100 or not 1<=args.columns<=20 or not 40<=args.cell<=1000: raise ValueError("Use <=100 images, 1..20 columns and cell 40..1000.")
    rows=math.ceil(len(paths)/args.columns)
    size=(args.columns*args.cell,rows*(args.cell+24))
    check_size(size)
    canvas=Image.new("RGB",size,"white")
    draw=ImageDraw.Draw(canvas)
    for i,path in enumerate(paths):
        thumb=ImageOps.contain(read_image(path),(args.cell-10,args.cell-10))
        x=(i%args.columns)*args.cell+(args.cell-thumb.width)//2
        y=(i//args.columns)*(args.cell+24)
        canvas.paste(thumb,(x,y),thumb)
        label=Path(path).name.encode("ascii","replace").decode("ascii")[:24]
        draw.text(((i%args.columns)*args.cell+5,y+args.cell),label,fill="black")
    save_image(args.output,canvas)

if __name__ == "__main__":
    try:
        main()
    except (OSError, ValueError, KeyError, TypeError, ArithmeticError, IndexError, LookupError) as exc:
        raise SystemExit(str(exc))

Open source as plain text · Download source and sample files

How it works

  1. Pillow reads the local image; the source is left unchanged and existing destinations are refused.
  2. Each image fits in its cell without cropping; names are shortened and rendered in a basic portable font.
  3. The example makes a 360 by 204 sheet containing the two fixtures.

Make it your own

--columns

Use --columns 4 to make a wider grid.

Adapt the processing

Edit main's label expression to use numbered captions instead of filenames.

Change one setting at a time, run it again on sample data, and compare the output. The customization guide walks through this process.

If something goes wrong

The canvas exceeds the pixel limit.

Reduce --cell, image count, or columns.

The destination already exists.

Choose a new --output path. The script deliberately refuses to replace an existing result.

Reference & next steps

These official references explain the underlying tools. The Scriptage example and guide are original instructional content. See verification notes for what has been checked and the limits of those checks.

Copies a template for your own notes. It does not send anything.