Skip to content

Instantly share code, notes, and snippets.

@remonsec
Created December 18, 2024 14:31
Show Gist options
  • Save remonsec/d3fa9b28373a81976971c326fa679140 to your computer and use it in GitHub Desktop.
Save remonsec/d3fa9b28373a81976971c326fa679140 to your computer and use it in GitHub Desktop.
add date & time to screenshot
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime
import sys
def add_date_to_screenshot(screenshot_path, output_path):
try:
# Open the screenshot
image = Image.open(screenshot_path)
draw = ImageDraw.Draw(image)
# Define the text (current date)
date_text = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Define font and size
font_size = max(10, image.width // 50)
font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", font_size)
# Get text bounding box
text_bbox = draw.textbbox((0, 0), date_text, font=font)
text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
# Define text position (bottom-right corner with some padding)
padding = 10
x = image.width - text_width - padding
y = image.height - text_height - padding
# Add semi-transparent rectangle behind text
rectangle_color = (0, 0, 0, 128) # Black with 50% opacity
rectangle_position = (x - padding // 2, y - padding // 2, x + text_width + padding // 2, y + text_height + padding // 2)
overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
overlay_draw = ImageDraw.Draw(overlay)
overlay_draw.rectangle(rectangle_position, fill=rectangle_color)
# Merge the rectangle and the image
image = Image.alpha_composite(image.convert("RGBA"), overlay)
draw = ImageDraw.Draw(image)
# Add the date text
text_color = (255, 255, 255, 255) # White
draw.text((x, y), date_text, font=font, fill=text_color)
# Save the updated screenshot
image.convert("RGB").save(output_path, "PNG")
print(f"Date added and saved to: {output_path}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python3 main.py <input_screenshot> <output_screenshot>")
sys.exit(1)
screenshot_path = sys.argv[1]
output_path = sys.argv[2]
add_date_to_screenshot(screenshot_path, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment