Change your wallpaper with Python

python-wallpaper
From a while I’ve been coding in Python. Exploring it’s potentialities . And they’re a lot, from topnotch frameworks like Django, to an excellent easy to read syntax, nice graphic libraries and a great participative community.
Today we I will show you how to create a script that allows you to change your desktop wallpaper with random backgrounds.( Windows only).



Download (1kb)

Everytime I start to learn a new language I like to create some simple scripts or applications, just to try to apply some of the knowledge I’ve acquired .
So, this time I will create a simple script that will fetch from a directory a random image and put it as your wallpaper.

Objective

  • Change the wallpaper every time we run the script
  • Let the user choose the searching directory as a parameter
  • Define as a startup script

Requirements

Code


import ctypes
import Image
import os
import sys
import random

These are the libraries that we will use, there it is Image aka PIL.
Now, I won’t lie, I’m pretty new at Python, so I really don’t know if Python brings a more simple Image lib, in these case to convert to .bmp, so for now we will use PIL.


def wallpaper_dir(path):
	try:
		directory = os.listdir(path)
	except WindowsError:
		print "Directory does not exist"
		exit(0)
	wallpapers = []
	types = ['jpg', 'png', 'gif']
	for file in directory:
		ext = file[-3::]
                if ext in types:
        		wallpapers.append(path + file)
	return wallpapers

This function we be responsible for the search on a specific directory for image files, in this case i’ve used only the most used types, jpg, gif and png.
It’s not a recursive function, so it will not search on sub directories. The main purpose here it’s to have a dir with a lot of images and let our script search for one,
I think the code speaks for himself, you can rapidly know what’s going on. The var directory will keep on a List every file found by the method os.listdir by the param Path of our function.

After that, it will go through the List and search for every .jpg, .png and .gif files, as in, the last 3 chars of each item ext = file[-3::]. I as you may guess, it will not validate if it’s an image or not, but you can do that pretty easily with PIL.


def set_wallpaper(wallpapers):
	random.seed()
	random.shuffle(wallpapers)
	wallpaper = random.sample(wallpapers,1)
	im = Image.open(wallpaper[0])
	im.save("wallpaper.bmp")
	MY_WALLPAPER = "wallpaper.bmp"
	SPI_SETDESKWALLPAPER = 20
	ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, MY_WALLPAPER , 0)

This is like the main function of our script, by the param wallpapers which in reality it’s nothing more than a List of the previous result, this function will randomly choose a file and covert it to .bmp, and using WIN32 API function it will change your wallpaper.
Again, the code speaks for itself, that’s on the things I love about Python, the code speaks for himself.


def main(argv):
	if len(sys.argv) > 1:
		sys.argv[1] = sys.argv[1] + "/"
		wallpapers = wallpaper_dir(sys.argv[1]);
		if len(wallpapers) > 0:
			set_wallpaper(wallpapers)
		else:
			print "No wallpapers were found."
	else:
		print "Usage: wallpaper.py <dir>"	

if __name__ == "__main__":
	main(sys.argv)

At last we have main() function, that will receive a parameter from the command line indicating where’s the wallpaper directory.

Now we just need to run it, e.g “wallpaper.py c:\wallpapers” and let it work his magic.

For the startup, all you need to do is a shortcut and drag it to your “startup” folder in “Start” windows menu.

I hope you’ve enjoyd it.

Download (1kb)

Share and Enjoy:

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • LinkedIn
  • Reddit
  • Slashdot
  • StumbleUpon
  • Twitter

Leave a Reply