Search on blog:

Python: Use pillow to create image with thumbnails in rows and columns

Simple script which gets as arguments pattern like path/*.jpg, number of rows, number of columns and create grid with images (thumbnails 100x100 pixels)

It uses pattern (instead path) with glob.glob() to get filenames, and pillow to load images, resize them and paste on output image as thumbnails in rows and columns.

import sys
import glob
from PIL import Image

# for test only 
#sys.argv += ['images/dot-*.jpg', 2, 3]

# get arguments
pattern = sys.argv[1]
rows = int(sys.argv[2])
cols = int(sys.argv[3])

# get filenames
filenames = glob.glob(pattern)
#print(filenames)

# load images and resize them
images = [Image.open(name).resize((100, 100)) for name in filenames]

# output image for grid with thumbnails
new_image = Image.new('RGB', (cols*100, rows*100))

# paste thumbnails on output
i = 0
for y in range(rows):
    if i >= len(images):
        break
    y *= 100
    for x in range(cols):
        x *= 100
        img = images[i]
        new_image.paste(img, (x, y, x+100, y+100))
        print('paste:', x, y)
        i += 1

# save output
new_image.save('output.jpg')
If you like it
Buy a Coffee