drstats/merge_images.py

49 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-11-23 18:56:52 +00:00
from PIL import Image
2024-11-23 19:17:01 +00:00
from pathlib import Path
import functools
import sys
2024-11-23 18:56:52 +00:00
2024-11-23 19:17:01 +00:00
printr = functools.partial(print, file=sys.stderr)
2024-11-23 18:56:52 +00:00
per_image_width = 480
per_image_height = 320
cols = 2
images = list(Path("./export/").glob("*.png"))
image_count = len(images)
2024-11-23 19:17:01 +00:00
total_image_height = image_count / cols * per_image_height
if image_count / cols * per_image_height > total_image_height:
2024-11-23 18:56:52 +00:00
total_image_height += per_image_height
2024-11-23 19:17:01 +00:00
total_image_width = image_count / cols * per_image_width
2024-11-23 18:56:52 +00:00
resized_images = []
for path in images:
image = Image.open(path)
image = image.resize((per_image_width, per_image_height))
2024-11-23 19:17:01 +00:00
resized_images.append((path, image))
2024-11-23 18:56:52 +00:00
2024-11-23 19:17:01 +00:00
new_image = Image.new(
"RGB",
(per_image_width * cols, int(per_image_height * image_count / cols)),
(250, 250, 250),
)
2024-11-23 18:56:52 +00:00
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)
2024-11-23 19:17:01 +00:00
top = int(per_image_height * current_row)
new_image.paste(image, (left, top))
2024-11-23 18:56:52 +00:00
new_image.save("export/1_graphs_compliation.png")
current_col += 1
current_image_number += 1
if current_col == cols:
current_col = 0
2024-11-23 19:17:01 +00:00
new_image.show()