Table of contents

How to program an automatic wallpaper changer in Python?

How to program an automatic wallpaper changer in Python?

In the previous post we made an automatic wallpaper changer in Python

In this entry we are going to use Cron to program the periodic execution of this script and that it is in charge of changing the wallpaper every certain time, automatically, every hour, two hours, every day, every minute or the frequency that we want.

If you don’t know how Cron daemon works and how to schedule tasks using this tool, please check my post about Cron and Crontab .

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

wallpaper_folder = "/home/user/imgs/wallpaper/" # Place your own directory here
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))
os.system("gsettings set org.gnome.desktop.background picture-uri-dark 'file://{}'".format(random_wallpaper))

Now, we are going to schedule its execution by means of Crontab.

If we place a Python script containing the above code block in Crontab and wait for the runtime to arrive, it will not work.

Adding environment variables to Crontab

Remember that when we schedule a task in Crontab, we do not have access to all environment variables.

For the wallpaper changer to work we will need to pass the environment variable called DBUS_SESSION_BUS_ADDRESS to the script we place in Crontab. To find the value of the environment variable we can do it from terminal, using the printenv command.

printenv | grep DBUS
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

Now that we have the value of the environment variable, we can add it to the environment variables via os.environ. Now this value will be available to Crontab when the script is run.

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

os.environ["DBUS_SESSION_BUS_ADDRESS"]="unix:path=/run/user/1000/bus" # LINEA NUEVA

wallpaper_folder = "/home/user/imgs/wallpaper/" # Place your own directory here
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))
os.system("gsettings set org.gnome.desktop.background picture-uri-dark 'file://{}'".format(random_wallpaper))

Schedule wallpaper change with Cron and Crontab

Once this is done, we are ready to add our script to Crontab.

crontab -e

For this example we will change the wallpaper every 6 hours. But you can set the frequency to any value you want.

0 */6 * * * $PWD/.change_wallpaper_random.py

You should also make sure that your file has the proper execution permissions, I have a post explaining the topic of chmod and permissions in GNU/Linux that you can check out.

Save the file and it should start running automatically every 6 hours, or whatever frequency you set, changing the wallpaper to a random one in the folder you specified in the Python script whether you’re using a Dark theme or a Light theme in newer versions of Gnome.

Eduardo Zepeda
Web developer and GNU/Linux enthusiast. I believe in choosing the right tool for the job and that simplicity is the ultimate sophistication. Better done than perfect. I also believe in the goodnesses of cryptocurrencies outside of monetary speculation.
Read more