Home > Backend Development > Python Tutorial > How to create a dynamic Wallpaper with Time and Date using Python

How to create a dynamic Wallpaper with Time and Date using Python

Patricia Arquette
Release: 2024-12-12 12:23:25
Original
809 people have browsed it

Cómo crear un Wallpaper dinámico con la Hora y Fecha usando Python

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.

What do we need?

  • Python installed: If you don't have Python installed yet, you can download it from python.org.

  • Install libraries:

    • Pillow: To work with images.
    • ctypes: To interact with the operating system and change the wallpaper in Windows.

You can install Pillow by running the following command in your terminal or cmd: pip install pillow

  • A font file: We need a TrueType font (.ttf) to render the text on the image. In the example code, we use the FiraCode font, but you can use any font you prefer.

1. Imports and initial configuration

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
Copy after login
Copy after login
  • Pillow (Image, ImageDraw, ImageFont) is used to create the image and draw the text.
  • ctypes is used to change the wallpaper in Windows.
  • time and datetime allow us to manage times and dates.
  • background_color and text_color are the colors that we will use for the background and text.

2. Formatting the date and time

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
Copy after login
Copy after login

For example, day "5" will be formatted as "05", and the same will happen with the hour, minute and second.

3. Generate the image with the date and time

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")}"
    }}
}}"""
Copy after login

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)
Copy after login

4. Set the image as wallpaper

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
Copy after login
Copy after login

Here, ctypes.windll.user32.SystemParametersInfoW is a Windows function that allows you to change the wallpaper.

5. Continuous updating of 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
Copy after login
Copy after login

This loop ensures that the wallpaper is updated every second with the current time and date.

Full code here: GitHub

Conclusion

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template