How to: Watermark photos with a days since date time stamp (Days since birth time stamp)

I recently had my first child and I found myself taking countless photos of my little one, but struggled to keep them organized. I wanted a simple and efficient way to keep track of his growth by watermarking each photo with his age since birth. That’s when I turned to Python and the Pillow library. With a simple script, I was able to copy the source directory of the photos and create a duplicate copy with the day count since his birth stamped in the bottom right corner.

This process not only helped me stay organized, but also allowed me to see his growth over time in a clear and concise way. If you want to try this out for yourself, simply install the Pillow library and use the provided Python script. This technique can also be used to keep track of your pet’s growth, relationship milestones, or any other event with a start date. So why not give it a try and enjoy your memories with a unique twist.

Required:
Pillow Library
ttf-mscorefonts-installer for Linux environments
(sudo apt-get install ttf-mscorefonts-installer)

from PIL import Image, ImageDraw, ImageFont
import os

date_str = input("Enter a date in the format yyyy-mm-dd: ")
day_num = int(date_str.split("-")[2])

source_folder = "/path/to/input/directory"
dest_folder = "/path/to/output/directory"

if not os.path.exists(dest_folder):
    os.makedirs(dest_folder)

for filename in os.listdir(source_folder):
    if filename.endswith(".jpg") or filename.endswith(".jpeg"):
        image_path = os.path.join(source_folder, filename)
        image = Image.open(image_path)

        font_path = "arial.ttf"  # Replace with your font file path
        font_size = 72  # Increase font size
        font = ImageFont.truetype(font_path, font_size)

        draw = ImageDraw.Draw(image)
        text_width, text_height = draw.textbbox((0, 0), str(day_num), font=font)[2:]
        draw.text((image.width-text_width-50, image.height-text_height-50), str(day_num), fill=(255, 255, 255), font=font)

        new_filename = f"{date_str}_{filename}"
        new_image_path = os.path.join(dest_folder, new_filename)
        image.save(new_image_path)

About the Author: Phil

Leave a Reply

Your email address will not be published. Required fields are marked *