Skip to content

Instantly share code, notes, and snippets.

@naranyala
Created December 28, 2024 14:29
Show Gist options
  • Save naranyala/94f5cff06635213dbc22ceb9b7cec93d to your computer and use it in GitHub Desktop.
Save naranyala/94f5cff06635213dbc22ceb9b7cec93d to your computer and use it in GitHub Desktop.
randomize windows wallpaper using python code
import os
import random
import ctypes
# Define the path to the directory containing wallpapers
WALLPAPER_DIRECTORY = "D:\\your-wallpaper-collection\\nested-dir\\"
def set_wallpaper(image_path):
"""
Sets the desktop wallpaper using the Windows API.
"""
# Use the Windows API to set the wallpaper
SPI_SETDESKWALLPAPER = 20
result = ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, 3)
if result:
print(f"Wallpaper set to: {image_path}")
else:
print("Failed to set wallpaper. Ensure the file exists and the path is correct.")
def randomize_wallpaper(directory):
"""
Randomly selects an image from the directory and sets it as the wallpaper.
"""
# Ensure the directory exists
if not os.path.exists(directory):
print(f"Directory does not exist: {directory}")
return
# Get a list of image files (extensions: .jpg, .jpeg, .png)
image_files = [f for f in os.listdir(directory) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
if not image_files:
print(f"No image files found in directory: {directory}")
return
# Choose a random image
selected_image = random.choice(image_files)
image_path = os.path.join(directory, selected_image)
# Set the wallpaper
set_wallpaper(image_path)
if __name__ == "__main__":
randomize_wallpaper(WALLPAPER_DIRECTORY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment