Fundamentals 4 min read

How to Randomly Change Your Windows Wallpaper with Python and ctypes

This guide shows Windows users how to programmatically change the desktop background using Python's ctypes library, walk through retrieving the script’s directory, filtering image files, selecting one at random, and applying it with SystemParametersInfoW, all without installing external packages.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Randomly Change Your Windows Wallpaper with Python and ctypes

Before starting, note that this article is intended for Windows users only.

It demonstrates how to change the desktop background in Python without installing external libraries, using the built‑in ctypes module to call the Windows API.

First, a simple class sets the wallpaper by calling SystemParametersInfoW with the path of an image file.

import ctypes

class Main:
    def __init__(self):
        path = 'c:/...'
        ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 0)

application = Main()

To make the script more flexible, it obtains the script’s directory using os and sys, then walks a folder named backgrounds to collect all PNG, JPG, and JPEG files.

import os
import sys

os.path.abspath(os.path.dirname(sys.argv[0]))
# e.g. 'C:\Users\user'

The final version randomly selects one image from the collected list and sets it as the wallpaper.

import os
import sys
import ctypes
import random

class Main:
    def __init__(self):
        self.path = os.path.abspath(os.path.dirname(sys.argv[0]))
        for root, directories, files in os.walk(os.path.join(self.path, 'backgrounds')):
            self.backgrounds = [file.lower() for file in files if file.endswith(('.png', '.jpg', '.jpeg'))]

        ctypes.windll.user32.SystemParametersInfoW(
            20, 0,
            os.path.join(self.path, 'backgrounds', random.choice(self.backgrounds)),
            0)

application = Main()

Place any desired images in the backgrounds folder; each execution of the script will temporarily set a random image as the desktop wallpaper.

For automatic execution at startup, copy the script into the shell:startup folder (opened via Win+R → “shell:startup”), so it runs each time Windows starts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Automationctypesdesktop wallpaper
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.