STEM

Customizing Windows desktop wallpaper automatically, on schedule (Python)

With a little programming know-how, you can write your own dynamic Windows wallpaper app that can change its color to whatever color you want, to a random color that your app chose for you for the day, or set to your photos, images on a rotating basis or randomly picked from a list from your Pictures list. In this post, I share the code required to set wallpaper colors, generate random colors for the wallpaper, apply specific images to the wallpaper without requiring any manual work, scheduling and more.


There is no Windows API to set a color directly for the wallpaper, but there is an API for setting it to a specific image. So, we can achieve the same effect by creating a bitmap image of a specific color using Pillow library, and then set the image as wallpaper using Windows API.

Setting the desktop wallpaper to a specific color

The ctypes library allows us to tap into the Windows API from Python and SystemParametersInfoW() function in Windows then lets us actually set the wallpaper.

SystemParametersInfoW() function parameters:
1st arg: SPI_SETDESKWALLPAPER is the constant for changing the desktop wallpaper.
2nd arg: For SPI_SETDESKWALLPAPER, 2nd argument is ignored, so set it to 0.
3rd arg: path to the image file to set as the new wallpaper.
4th arg: It specifies how the system parameter should be set. 3 means that the change should be saved in the user’s profile and that a WM_SETTINGCHANGE message should be broadcast to all top-level windows to notify them of the change.

Be sure to import ctypes library, and install PIL library first in your Python environment…it’s a free add-on. The complete code is shown below:

I have added comments in the code where I think it would be helpful to elaborate.

Now, let’s move on to the next task on hand: apply an image to the wallpaper programmatically.

Setting the desktop wallpaper to a specific image

Be sure to import ctypes library, and install PIL library first in your Python environment. The complete code is shown below:

Note that these changes are immediate; there is no need to reboot to see the changes. And you can run either or both scripts above whenever you wish. The scripts above must be executed with administrative priviledge.

Taking it a step farther

A few additional features I have added to the above (those additional codes are not shown here):

  1. Added code to log the date and the pseudo-random color generated into a CSV file as append every time code is executed. This makes it possible to analyze how many times, which colors were picked and set over time. I also chose to save the date as a 4-digit serial number as Excel does, that way, it’s more compact and can be formatted to whatever locale and format I want, and can even do date calculations on them. To learn more about how to do that date conversion, see my post: https://flyingsalmon.net/how-to-convert-a-standard-date-format-excel-date-serial-number/
  2. Added code to read the image to set as wallpaper from a file that contains a full list of images to pick from. Then pick a random one from that file each time. This keeps the wallpaper dynamic.
  3. Scheduled the script to run daily at a specific time. To schedule this to run automatically, I used Windows Task Scheduler, and point to the .py script. This way, wallpaper is fresh and I don’t have to manually run the script daily.

If you’re interested in those additional features, full source code, and instructions on how to schedule them, drop me a line to trseattle at outlook dot com. People who made any donations via Paypal are eligible to receive them at no charge upon request.

I hope this was educational and interesting. Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top