This repository has been archived on 2024-12-19. You can view files and clone it, but cannot push or open issues or pull requests.
zombietextures/timeatbuild.py

49 lines
2.2 KiB
Python

from PIL import Image, ImageDraw, ImageFont
import time
import datetime
def overlay_text_on_image(background_image_path):
# Open the background image
img = Image.open(background_image_path)
d = ImageDraw.Draw(img)
# Get the current UNIX time
unix_timestamp = time.time()
unix_time_str = f"{unix_timestamp}"
# Get the current system time
system_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
system_time_str = f"{system_time}"
# Get date formated as 23d365
systemzdate = datetime.datetime.now()
systemzyear = systemzdate.year % 100
systemz_day_of_year = systemzdate.timetuple().tm_yday
systemz_time_str = f"a{systemzyear}d{systemz_day_of_year}"
# Use a built-in font (or replace with your own font path)
fnt = ImageFont.truetype("DejaVuSans.ttf", 14)
fntz = ImageFont.truetype("DejaVuSans.ttf", 16)
# Calculate text position (centered)
unix_time_bbox = d.textbbox((0, 0), unix_time_str, font=fnt)
system_time_bbox = d.textbbox((0, 0), system_time_str, font=fnt)
systemz_time_bbox = d.textbbox((0, 0), systemz_time_str, font=fntz)
unix_time_width, unix_time_height = unix_time_bbox[2] - unix_time_bbox[0], unix_time_bbox[3] - unix_time_bbox[1]
system_time_width, system_time_height = system_time_bbox[2] - system_time_bbox[0], system_time_bbox[3] - system_time_bbox[1]
systemz_time_width, systemz_time_height = systemz_time_bbox[2] - systemz_time_bbox[0], systemz_time_bbox[3] - systemz_time_bbox[1]
img_width, img_height = img.size
d.text(((img_width - unix_time_width) / 2, (1 * img_height / 6) - unix_time_height / 2), unix_time_str, font=fnt, fill="white")
d.text(((img_width - system_time_width) / 2, (3 * img_height / 6) - system_time_height / 2), system_time_str, font=fnt, fill="white")
d.text(((img_width - systemz_time_width) / 2, (5 * img_height / 6) - systemz_time_height / 2), systemz_time_str, font=fnt, fill="white")
# Save the image with the overlay text
img.save('overlay_datetime_image.png')
if __name__ == "__main__":
background_image_path = 'timebackground.png' # Replace with your image path
overlay_text_on_image(background_image_path)