Table of contents

How to create a wallpaper changer using Python in Gnome?

How to create a wallpaper changer using Python in Gnome?

In this post we are going to create an automatic, random and minimalistic wallpaper changer for GNU/Linux using Python. With no extra functions, super lightweight and totally homemade, its only function will be to randomly select an image and set it as wallpaper. I will explain the function of each line in the code.

Note: I am running this code in GNOME 3.22.3 and Python 3.5.3.

If you are not familiar with Python syntax, read about one of the best books for getting into Python in my post about the book Python Immersion.

To begin with let’s first change the directory to home/

cd ~

Once here we are going to create a Python file with the name .change_wallpaper_randomly.py but you can give it any other name, just make sure that the name starts with a dot “.” In GNU/Linux all files that start with a dot are hidden files, returning our script a hidden file will serve us later.

touch .change_wallpaper_randomly.py

If at this point you feel you are not familiar with these GNU/Linux commands you can read my post on basic GNU/Linux commands to refresh your memory.

Change wallpaper automatically with Python

Now inside the file we have just created we are going to place the following code

#!/usr/bin/python3
import os
import random

wallpaper_folder = "/home/usuario/Imágenes/wallpaper/" # Coloca aquí tu propia ruta
os.chdir(wallpaper_folder)
allowed_image_formats = ["jpg", "png", "jpeg"]
list_of_images = [image for image in os.listdir() if image.endswith(tuple(allowed_image_formats))]
random_wallpaper = os.path.join(os.getcwd(), random.choice(list_of_images))
os.system("gsettings set org.gnome.desktop.background picture-uri 'file://{}'".format(random_wallpaper))

First we import the os and random libraries, to have access to tools to interact with the operating system and methods for random numbers, respectively.

Next we assign the path where the images are located to the variable wallpaper_folder, this path is defined by you, it is the location of your folder with the images you want to use as wallpaper. For this example make sure you use an absolute path.

The os.chdir() method will allow us to change to the directory where we have the images that we will use as wallpapers.

The allowed_image_formats variable will set which image formats we will accept as wallpapers.

The next line is a little more complex and, from the syntax, you can see that it is a list comprehension:

list_of_images = [image for image in os.listdir() if image.endswith(tuple(allowed_image_formats))]

os.listdir() will return a list of all the files in our current folder, i.e. all those wallpapers contained in the folder we switched to with os.chdir().

With tuple(allowed_image_formats) we are simply transforming our list of allowed formats into a tuple.

The image.endswith() method takes an argument, which can be a text string or a tuple of text strings, and will return True or False depending on whether the image object ends the text string with any of the elements of the tuple.

The complete line is a list comprehension that says: “create a list with each image in os.listdir() as long as the file ends with any of the following formats in this tuple”. This step is important because it will exclude all non-image files from our list.

Now, the following line may also appear to be somewhat complex

random_wallpaper = os.path.join(os.getcwd(), random.choice(list_of_images))

The os.path.join() method is in charge of joining paths in our operating system and is totally agnostic of the operating system in which we are. We will join the path of the current directory with the name of an image, to generate the complete path of an image.

os.getcwd() returns the current directory.

Select an image at random with random.choice()

We will randomly obtain the image by means of the random.choice() method, which selects a random image from all those obtained with the list comprehension.

Look at this example:

lista_de_imagenes_de_prueba = ["imagen_1.jpg", "imagen_2.jpg", "imagen_3.png", "imagen_4.png", "imagen_5.jpeg"]
random.choice(lista_de_imagenes_de_prueba)
"imagen_4.png"
random.choice(lista_de_imagenes_de_prueba)
"imagen_1.jpg"
random.choice(lista_de_imagenes_de_prueba)
"imagen_1.jpg"

And finally

os.system("gsettings set org.gnome.desktop.background picture-uri 'file://{}'".format(random_wallpaper))

The os.system() method allows us to execute a command in our terminal.

We will run a command that will tell gnome to set an image as wallpaper, we will pass the path to that image after picture-uri.

Note here, each desktop environment will have a way to set a wallpaper, if you want to use another desktop environment just read the documentation and replace the command inside the os.system() method.

Once we are done, we can run our script as follows. Make sure you are in home.

python3 .change_wallpaper_randomly.py

If everything went correctly your wallpaper will have changed to a random image in the folder you specified. You can run the script as many times as you want and you will see how your wallpaper will change over and over again with each run.

But having to run this command every time we want to change wallpaper is quite cumbersome, wouldn’t it be great to schedule it to run every so often?

If you already read my post about crontab and cron you already have an idea of how to achieve this, skip to schedule the periodic execution of this script using crontab.

Eduardo Zepeda
Web developer and GNU/Linux enthusiast always learning something new. I believe in choosing the right tool for the job and that simplicity is the ultimate sophistication. I'm under the impression that being perfect is the enemy of getting things done. I also believe in the goodnesses of cryptocurrencies outside of monetary speculation.
Read more