Skip to content

Add Date & Timestamp to Still and Time Lapse captured images. #1488

@LucidEye

Description

@LucidEye

I believe it would be very useful to many users if Mycodo had a feature which would overlay a basic date & timestamp on the Still and Time Lapse images. Using time lapse videos is very useful in calculating when watering times and intervals should be set to avoid water column collapse and wilting in plants... it would be very helpful in this respect to have an accurate timestamp overlayed on Still and Time Lapse captured images without having to use 3rd-party code or an external app. Trying to use a 3rd-party Linux app to add a timestamp to an already existing image is clunky and time-consuming at best and really doesn't work when running on the Pi parallel to Mycodo, especially when Mycodo is capturing a new image every few seconds for a time lapse video. And trying to offload those pics to a PC and add timestamps to an image sequence of thousands of images taken for compiling into a time-lapse video is excruciatingly slow and cumbersome... Is there some reason that Mycodo can't place a timestamp on it's image captures?
Was thinking of something like this timestamp at the top of the image...

Image

Supposedly this code-block recommended by Google can add a timestamp to images... can this be added to the Camera Function?

from datetime import datetime

def add_timestamp_to_image(image_path, output_path, font_size=30, text_color=(255, 255, 255), position=(10, 10)):
    """
    Adds a current timestamp to an image.

    Args:
        image_path (str): The path to the input image.
        output_path (str): The path to save the output image with the timestamp.
        font_size (int): The size of the timestamp text.
        text_color (tuple): The RGB color of the timestamp text (e.g., (255, 255, 255) for white).
        position (tuple): The (x, y) coordinates where the timestamp should be placed.
    """
    try:
        # Open the image
        img = Image.open(image_path)
        draw = ImageDraw.Draw(img)

        # Get current timestamp
        timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        # Define font (you might need to provide a path to a .ttf file if you want a specific font)
        try:
            font = ImageFont.truetype("arial.ttf", font_size) # Using a common font like Arial
        except IOError:
            font = ImageFont.load_default() # Fallback to default font if Arial not found

        # Add the timestamp to the image
        draw.text(position, timestamp, font=font, fill=text_color)

        # Save the modified image
        img.save(output_path)
        print(f"Timestamp added successfully to {output_path}")

    except FileNotFoundError:
        print(f"Error: Image file not found at {image_path}")
    except Exception as e:
        print(f"An error occurred: {e}")

# Example usage:
if __name__ == "__main__":
    input_image = "input.jpg"  # Replace with your input image file
    output_image = "output_with_timestamp.jpg"

    # Create a dummy image for testing if input.jpg doesn't exist
    try:
        Image.new('RGB', (600, 400), color = 'red').save(input_image)
        print(f"Created a dummy image: {input_image}")
    except Exception as e:
        print(f"Could not create dummy image: {e}")

    add_timestamp_to_image(input_image, output_image)```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions