Marseygen/utils.py

25 lines
791 B
Python

from PIL import Image, ImageOps
#
# https://gist.github.com/njanakiev/1932e0a450df6d121c05069d5f7d7d6f
#
def concat_images(image_paths, size, shape=None):
# Open images and resize them
width, height = size
images = map(Image.open, image_paths)
images = [ImageOps.fit(image, size, Image.ANTIALIAS) for image in images]
# Create canvas for the final image with total size
shape = shape if shape else (1, len(images))
image_size = (width * shape[1], height * shape[0])
image = Image.new("RGB", image_size)
# Paste images into final image
for row in range(shape[0]):
for col in range(shape[1]):
offset = width * col, height * row
idx = row * shape[1] + col
image.paste(images[idx], offset)
return image