Monday, February 05, 2007

Set Windows Wallpaper from Python

*** For multiple monitors see this post instead Set windows wallpaper from python for multiple monitors  ***


Here's some code to set a random wallpaper from a list of images, or a list of directories or a single image. It doesn't recurse subdirectories, but, that's just as simple to add if you really require that functionality. It requires PIL.

You can set it to change the wallpaper to rotate to a new one at a specified number of minutes.

It can use any image that PIL can read (it converts it to a BMP).

# Code to rotate or set wallpaper under windows
# Copyright (C) Andrew K. Milton 2007
# Released under a 2-clause BSD License
# See: http://www.opensource.org/licenses/bsd-license.php

import os
import time
import random

from optparse import OptionParser
from ConfigParser import SafeConfigParser
from ctypes import windll

from win32con import *
from PIL import Image

"""
I recommend you create a pywallpaper.conf file that looks something like this;
[directories]
paths = C:\Documents and Settings\akm\My Documents\My Pictures\gb
C:\Documents and Settings\akm\My Documents\My Pictures\Ralph

to store the directories in rather than specifying -d multiple times on the command line"""


def getDefaultDirs():
return [r'C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures',]

def setWallPaperFromBmp(pathToBmp):
""" Given a path to a bmp, set it as the wallpaper """
result = windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0,
pathToBmp,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE)
if not result:
raise Exception("Unable to set wallpaper.")


def setWallPaper(pathToImage):
""" Given a path to an image, convert it to bmp format and set it as the wallpaper"""

bmpImage = Image.open(pathToImage)
newPath = os.getcwd()
newPath = os.path.join(newPath, 'pywallpaper.bmp')
bmpImage.save(newPath, "BMP")
setWallPaperFromBmp(newPath)

def setWallPaperFromFileList(pathToDir):
""" Given a directory choose an image from it and set it as a wallpaper """
# Image directories often contain Thumbs.db or other non-image
# files or directories, try a few times to set a wallpaper, and then just give up.

tries = 0
done = False

while (not done) and tries < 3:
try:
files = os.listdir(pathToDir)
image = random.choice(files)
setWallPaper(os.path.join(pathToDir, image))
done = True
except:
tries += 1
return done

def setWallPaperFromDirList(pathList):
""" Given a list of directories choose a directory """
imageDir = random.choice(pathList)
status = setWallPaperFromFileList(imageDir)

# Don't really need to say anything if we couldn't set the
# wallpaper, I think the user will notice

def getImageDirectories(options):
configFile = 'pywallpaper.conf'

if options.configFile:
configFile = options.configFile

dirs = options.directories

try:
config = SafeConfigParser()
config.readfp(open(configFile))
dirs += config.get('directories', 'paths').split('\n')
except:
pass

if not dirs:
# raise Exception("No directories defined")
dirs = getDefaultDirs()
return dirs


def setWallPaperFromConfigDirs(options):
setWallPaperFromDirList(getImageDirectories(options))

def getCommandLineOptions():
parser = OptionParser()
parser.add_option("-t", "--time", dest="change_time",
help = "Change wallpaper time in minutes (0 = change once and exit [default])",
default = 0, type = "int")
parser.add_option("-i", "--image", dest="singleImage", default = None,
help = "Set wallpaper to this image and exit (overrides -d)")

parser.add_option("-d", "--directory", dest="directories", default = [],
action="append", type="string",
help = "Add an image directory")

parser.add_option("-c", "--config", dest="configFile", default = None,
help = "path to alternate config file (default <working dir>/pywallpaper.conf)")

parser.add_option("-w", "--workingdir", dest="cwd", default=".",
help = "Working Directory (default .)")

(options, args) = parser.parse_args()
return (options, args)

if __name__ == '__main__':

options, args = getCommandLineOptions()

if options.cwd and options.cwd != '.':
os.cwd(cwd)

if options.singleImage:
setWallPaper(options.singleImage)
elif not options.change_time:
setWallPaperFromConfigDirs(options)
else:
dirs = getImageDirectories(options)
sleepTime = options.change_time * 60.0
while True:
setWallPaperFromDirList(dirs)
time.sleep(sleepTime)

4 comments:

fio said...

Thanks for this, very useful!

U said...

I think these changes will not persist system restart since registry keys

HKEY_CURRENT_USER/Control Panel/Desktop
Wallpaper REG_SZ
ConvertedWallpaper REG_SZ
OriginalWallpaper REG_SZ
WallpaperStyle REG_SZ

have not been changed

akm said...

The wallpaper persists just fine...
On XP, Vista and Windows-7

akm said...

I have a more comprehensive multi-monitor version too. Monitors can be in ANY configuration, not just stacked, they can have overlapping offsets.

I should do an updated post.