Code a Time-lapse Wallpaper in Python 3 for Windows 10

What’s going on guys?

I got a nice and short tutorial this time for coding a time-lapse wallpaper using Python 3 – kind of like a customizable live wallpaper.

If you’ve followed my recent posts, you might remember of a Python background changer for dual monitors I made a while back.

Well, this tutorial is kind of building up on that post. Remember, if you just want to download the files, head down to the end of the post.

Time-lapse Wallpaper

Now that pretty much any smartphone already comes with built-in time-lapse features in their cameras and software, I thought:

“Wouldn’t it be cool to have a little script to instantly turn any time-lapses into a wallpaper? That can’t be too difficult!” 😀

That is basically the story for this video tutorial.

It turns out, it wasn’t too difficult after all, just about ~40 lines of code in Python 3 – with some comments here and there, so maybe even 30ish.

Dive Into The Code

If you want a good explanation of the code, I recommend you check out my previous post (or this video) on changing the background for dual monitors. That post goes more in-depth line by line into the same exact concepts.

Now let’s peep the code:

import os
import sys
import glob
import time
import win32api, win32con, win32gui

# Variable to store all images from directory
dir1 = []

# Let's move into dir that contains images and script
os.chdir("C:\\Users\\User\\Desktop\\bgs\\timelapse-sea1\\")

# Get all timelapse images
def getImages():
	global dir1
	dir1 = glob.glob('./*.png')

# Function to actually set the wallpaper
def setWallpaper(path):
    key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(key, "WallpaperStyle", 0, win32con.REG_SZ, "0")
    win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, path, 1+2)

# Let's get all images from directory
getImages()

# Main program loop to switch images
x = 0
try:
	while True:
		# Let's get image X from directory list
		img1 = dir1[x]

		# Set the wallpaper as tiled image
		path = "C:\\Users\\User\\Desktop\\bgs\\timelapse-sea1\\" + img1
		setWallpaper(path)	# Change wallpaper
		x += 1				# Move onto the next image
		if x > len(dir1) or x == len(dir1): x = 0

		# Sleep for any amount of time (in this case, 0.01ms)
		time.sleep(0.05)
except KeyboardInterrupt:
	print('\n> Finished.')

To setup the whole thing just create a folder and place the time-lapse images inside of it – more details about how to extract the images in the video I posted. I recommend placing the script in the same folder.

Afterwords, change the directory on the script both in the first few lines of code and the ‘path‘ variable as well.

Finally, you’ll want to adjust the amount of time to sleep (I’ve left my script at 5 milliseconds, for more realism do 1 millisecond). Note that this code might slow down your computer the lower the time to sleep.

Download Code

If you have any questions, leave a comment below.

Hope y’all enjoyed this video and see you next time! 😉

Share: