Skip to content

Instantly share code, notes, and snippets.

@eerkunt
Last active June 8, 2024 12:59
Show Gist options
  • Save eerkunt/8ba7a9e16407e8b8c3330a011e9a3d90 to your computer and use it in GitHub Desktop.
Save eerkunt/8ba7a9e16407e8b8c3330a011e9a3d90 to your computer and use it in GitHub Desktop.
Combine all given keogram files horizontally
from PIL import Image, ImageDraw, ImageFont
import os
from collections import defaultdict
import calendar
def merge_keograms(directory, output_file):
print("Starting to merge keograms...")
files = [f for f in os.listdir(directory) if f.endswith('.jpg') and os.path.isfile(os.path.join(directory, f))]
files.sort()
print(f"Found {len(files)} keogram files.")
keograms_by_month = defaultdict(list)
for file in files:
date_part = file.split('-')[1].split('.')[0]
year_month = date_part[:6]
keograms_by_month[year_month].append(file)
monthly_images = []
for year_month, month_files in sorted(keograms_by_month.items()):
print(f"Processing month: {year_month} with {len(month_files)} files.")
first_image = Image.open(os.path.join(directory, month_files[0]))
width, height = first_image.size
total_width = width * 30
month_image = Image.new('RGB', (total_width, height))
x_offset = 0
for i, file in enumerate(month_files):
print(f" - Merging file: {file}")
img = Image.open(os.path.join(directory, file))
month_image.paste(img, (x_offset, 0))
# Add day of the month text
draw = ImageDraw.Draw(month_image)
day_of_month = i + 1
day_text = str(day_of_month)
font = ImageFont.truetype("../fonts/Orbitron-Medium.ttf", 50) # Adjust font size as needed
text_x = x_offset + 10 # Adjust text position as needed
text_y = 10
draw.text((text_x, text_y), day_text, font=font, fill=(255, 255, 255, 128))
x_offset += img.size[0]
# Add the month and year text
draw = ImageDraw.Draw(month_image)
year = year_month[:4]
month = int(year_month[4:6])
month_name = calendar.month_name[month]
text = f"{month_name} {year}"
# Determine the font size to fit the text to the image width
max_width = total_width * 0.9
font_size = 300
font = ImageFont.truetype("../fonts/Orbitron-Medium.ttf", font_size)
text_x = 250
text_y = ((height - draw.textbbox((0, 0), text, font=font)[3]) // 4)
draw.text((text_x, text_y), text, font=font, fill=(200, 200, 200, 128))
temp_file = os.path.join(directory, f"{year_month}_combined.jpg")
month_image.save(temp_file)
monthly_images.append(temp_file)
print(f" - Saved combined image for {year_month}")
first_month_image = Image.open(monthly_images[0])
month_width, month_height = first_month_image.size
total_height = month_height * len(monthly_images)
final_image = Image.new('RGB', (month_width, total_height))
y_offset = 0
for month_image_file in monthly_images:
print(f"Combining month image: {month_image_file}")
month_image = Image.open(month_image_file)
final_image.paste(month_image, (0, y_offset))
y_offset += month_height
final_image.save(output_file)
print(f"Combined keogram saved as {output_file}")
for month_image_file in monthly_images:
os.remove(month_image_file)
print(f"Removed temporary file: {month_image_file}")
merge_keograms(".", "~/combined_keograms.jpg") # Use source path and dest path of your liking here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment