Last modified: Apr 21, 2025 By Alexander Williams

Python Image Collages Guide

Creating image collages is fun with Python. This guide shows you how. Use libraries like PIL and OpenCV.

Why Create Collages in Python?

Python makes collage creation easy. Automate the process. Save time on manual editing.

Useful for photo albums, social media, or data visualization. Perfect for batch processing many images.

Required Libraries

You need these Python libraries:

  • Pillow (PIL)
  • OpenCV (optional)
  • NumPy (optional)

Install them using pip:


pip install pillow opencv-python numpy

Basic Collage Creation

Start with simple grid collages. Use PIL's Image class. Combine images side by side.


from PIL import Image

# Open images
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg")

# Create new blank image
collage = Image.new('RGB', (img1.width + img2.width, max(img1.height, img2.height)))

# Paste images
collage.paste(img1, (0, 0))
collage.paste(img2, (img1.width, 0))

# Save result
collage.save("simple_collage.jpg")

This code creates a horizontal collage. Adjust widths for vertical layouts.

Advanced Grid Collages

For more complex grids, calculate positions. Use loops for multiple images.


def create_grid(images, rows, cols):
    # Calculate collage size
    width = max(img.width for img in images) * cols
    height = max(img.height for img in images) * rows
    
    collage = Image.new('RGB', (width, height))
    
    # Paste images in grid
    for i, img in enumerate(images):
        row = i // cols
        col = i % cols
        x = col * img.width
        y = row * img.height
        collage.paste(img, (x, y))
    
    return collage

This function handles any grid size. Pass your images as a list.

Adding Borders and Spacing

Make collages look professional. Add borders between images.


def create_spaced_grid(images, rows, cols, spacing=10):
    # Calculate sizes with spacing
    img_width = max(img.width for img in images)
    img_height = max(img.height for img in images)
    
    width = (img_width * cols) + (spacing * (cols - 1))
    height = (img_height * rows) + (spacing * (rows - 1))
    
    collage = Image.new('RGB', (width, height), color=(255, 255, 255))
    
    # Paste images with spacing
    for i, img in enumerate(images):
        row = i // cols
        col = i % cols
        x = col * (img_width + spacing)
        y = row * (img_height + spacing)
        collage.paste(img, (x, y))
    
    return collage

The spacing parameter controls gap size. White background shows between images.

Creative Collage Layouts

Go beyond grids. Create artistic arrangements. Overlap images or use shapes.

For advanced layouts, try OpenCV. Combine it with PIL for best results.

Check our Python Image Processing Guide for more techniques.

Adding Text to Collages

Label your collages. Use PIL's text drawing functions.


from PIL import Image, ImageDraw, ImageFont

def add_text(collage, text, position, font_size=20):
    draw = ImageDraw.Draw(collage)
    
    try:
        font = ImageFont.truetype("arial.ttf", font_size)
    except:
        font = ImageFont.load_default()
    
    draw.text(position, text, fill="white", font=font)
    return collage

Position text anywhere. Customize font and color.

Saving Collages

Save in different formats. Control quality for JPEGs.


# Save as high quality JPEG
collage.save("output.jpg", quality=95)

# Save as PNG (lossless)
collage.save("output.png")

For more on formats, see our Python Image Encoding Guide.

Performance Tips

Handle large collages efficiently. Resize images first if needed.

Our Python Image Rescaling Guide shows how to resize properly.

Process images in batches for large projects. Use threading for speed.

Conclusion

Python makes image collage creation easy. Start with simple grids. Move to advanced layouts.

The PIL library offers all needed tools. Combine with OpenCV for more options.

Experiment with different layouts. Automate your photo workflows.