Skip to content

Instantly share code, notes, and snippets.

@ritiek

ritiek/change-wallpaper.py

Last active Jun 28, 2019
Embed
What would you like to do?
A Python cron-job that automatically sets desktop wallpapers from my Box cloud account
#!/usr/bin/python
import random
import os
import subprocess
import sys
from PIL import Image
ROOT_DIR = os.path.expanduser("~/Mount/box")
WALLPAPER_DIR = os.path.join(ROOT_DIR, "Pictures")
CHANGE_WALLPAPER_CMD = "gsettings set org.gnome.desktop.background picture-uri 'file://{}'"
MIN_RESOLUTION = (1920, 1080)
def get_random_file(directory):
files = os.listdir(directory)
chosen_file = random.choice(files)
return os.path.join(directory, chosen_file)
def set_wallpaper(wallpaper):
command = CHANGE_WALLPAPER_CMD.format(wallpaper)
return subprocess.call(command.split())
if not os.path.exists(WALLPAPER_DIR):
sys.exit(1)
while True:
wallpaper = get_random_file(WALLPAPER_DIR)
img = Image.open(wallpaper)
width, height = img.size
if (width, height) >= MIN_RESOLUTION:
set_wallpaper(wallpaper)
print(wallpaper)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.