from PIL import Image from pathlib import Path import functools import sys printr = functools.partial(print, file=sys.stderr) per_image_width = 480 per_image_height = 320 cols = 2 images = list(Path("./export/").glob("*.png")) image_count = len(images) total_image_height = image_count / cols * per_image_height if image_count / cols * per_image_height > total_image_height: total_image_height += per_image_height total_image_width = image_count / cols * per_image_width resized_images = [] for path in images: image = Image.open(path) image = image.resize((per_image_width, per_image_height)) resized_images.append((path, image)) new_image = Image.new( "RGB", (per_image_width * cols, int(per_image_height * image_count / cols)), (250, 250, 250), ) current_col = 0 current_row = 0 current_image_number = 0 for path, image in resized_images: printr("Merging image {}".format(path)) current_row = int(current_image_number / cols) left = int((current_col) * per_image_width) top = int(per_image_height * current_row) new_image.paste(image, (left, top)) new_image.save("export/1_graphs_compliation.png") current_col += 1 current_image_number += 1 if current_col == cols: current_col = 0 new_image.show()