If you've ever wanted to have a wallpaper that automatically updates with the current time and date, this tutorial is for you. We will use Python and some libraries like PIL (Pillow) to generate an image, add dynamic text with time and date, and then set this image as wallpaper on Windows.
Python installed: If you don't have Python installed yet, you can download it from python.org.
Install libraries:
You can install Pillow by running the following command in your terminal or cmd: pip install pillow
At the beginning of the script, we import the necessary libraries and define some initial parameters such as background color, text color, font size and font file path:
from PIL import Image, ImageDraw, ImageFont import ctypes, os, time, io from datetime import datetime background_color = (0, 0, 0) # Color de fondo (negro) text_color = (22, 230, 69) # Color del texto (verde) font_size = 30 font_path = r"c:/Users/david/Desktop/FiraCodeRegular.ttf" # Ruta de la fuente font = ImageFont.truetype(font_path, font_size) screen_width = 1920 screen_height = 1080
We define a function to ensure that the date and time numbers are always formatted with two digits:
def two_digits_format(number): return f"0{number}" if number < 10 else number
For example, day "5" will be formatted as "05", and the same will happen with the hour, minute and second.
The add_text_to_image() function creates a new blank image with the size of the screen (in this case, 1920x1080) and draws the text in the center:
def agregar_texto_a_imagen(): now = datetime.now() texto = f"""{{ "date": {{ "day": {two_digits_format(now.day)}, "month": {two_digits_format(now.month)}, "year": {now.year} }}, "time": {{ "hour": {two_digits_format(now.hour)}, "minute": {two_digits_format(now.minute)}, "second": {two_digits_format(now.second)}, "meridiem": "{now.strftime("%p")}" }} }}"""
In this function, we get the current date and time using datetime.now(), and then format the text into JSON format. This is rendered in the image we generated.
Then, we calculate the center of the screen to place the text there:
bbox = draw.textbbox((0, 0), texto, font=font) ancho_texto = bbox[2] - bbox[0] altura_texto = bbox[3] - bbox[1] posicion_x = (ancho_imagen - ancho_texto) // 2 posicion_y = (altura_imagen - altura_texto) // 2 posicion = (posicion_x, posicion_y)
The set_as_wallpaper() function saves the generated image as a temporary file and then sets it as the wallpaper:
from PIL import Image, ImageDraw, ImageFont import ctypes, os, time, io from datetime import datetime background_color = (0, 0, 0) # Color de fondo (negro) text_color = (22, 230, 69) # Color del texto (verde) font_size = 30 font_path = r"c:/Users/david/Desktop/FiraCodeRegular.ttf" # Ruta de la fuente font = ImageFont.truetype(font_path, font_size) screen_width = 1920 screen_height = 1080
Here, ctypes.windll.user32.SystemParametersInfoW is a Windows function that allows you to change the wallpaper.
In the while True: loop, the image is generated and set as wallpaper once per second:
def two_digits_format(number): return f"0{number}" if number < 10 else number
This loop ensures that the wallpaper is updated every second with the current time and date.
Full code here: GitHub
This code offers a simple way to generate a dynamic wallpaper that always shows the current time and date. You can customize the appearance of the wallpaper by adjusting the colors, font, and image size. Additionally, the code is designed to be efficient, with the wallpaper updating occurring every second.
If you're looking for a fun way to learn about image manipulation in Python and how to interact with the operating system, this project is a great starting point.
The above is the detailed content of How to create a dynamic Wallpaper with Time and Date using Python. For more information, please follow other related articles on the PHP Chinese website!