Simple photo collage on Linux

I was looking today for a tool to create a simple collage from a set of pictures on Linux, basically a simple 3×3 grid. I was not keen on using an interactive tool like Gimp since I wanted to be able to tweak the list of pictures or the size of the grid easily. Unfortunately, I did not find anything convincing for this specific use case, but it was quite easy to program in Python using PIL:

from PIL import Image, ImageDraw
import os
import math

collage_width = 6240
collage_height = 4160
collage_rows = 3
collage_columns = 3

root_path = '/path/to/single/pictures/dir'
file_names = [
    'file1.jpg',
    'file2.jpg',
    'file3.jpg',
    'file4.jpg',
    'file5.jpg',
    'file6.jpg',
    'file7.jpg',
    'file8.jpg',
    'file9.jpg',
]

save_path = "collage.jpg"

collage = Image.new("RGB", (collage_width, collage_height), color=(255, 255, 255))

size_x = math.ceil(collage_width / collage_columns)
size_y = math.ceil(collage_height / collage_rows)
collage_n_pics = collage_rows * collage_columns

for index in range(0, collage_n_pics):
    print(f"Loading image {index}/{collage_n_pics}")
    file_path = os.path.join(root_path, file_names[index])
    photo = Image.open(file_path).convert("RGB")
    photo = photo.resize((size_x, size_y))
    position_x = (index % collage_columns) * size_x
    position_y = int(index / collage_columns) * size_y
    collage.paste(photo, (position_x, position_y))

collage.save(save_path)
print(f'Collage saved as "{save_path}"')

Just adapt the variables at the beginning of the program to your own needs. The assumption is that all single images and the final collage have the same as aspect ration, otherwise there will be some holes or overlapping.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.