Skip to content

Instantly share code, notes, and snippets.

@ritiek
Last active June 28, 2019 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritiek/02d462ce9e649a5203d5b766cefbf4c9 to your computer and use it in GitHub Desktop.
Save ritiek/02d462ce9e649a5203d5b766cefbf4c9 to your computer and use it in GitHub Desktop.
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