Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created May 24, 2020 05:23
Show Gist options
  • Save nick3499/78b449830b29e706ecb14a461336fc8f to your computer and use it in GitHub Desktop.
Save nick3499/78b449830b29e706ecb14a461336fc8f to your computer and use it in GitHub Desktop.
Set background image and display mode in gsettings: subprocess.run(), click
#! /bin/python3
'''`set_bg_img` module contains `set_img()` method which sets image URI and display mode.
$ python3 set_bg_img.py'''
from subprocess import run
from click import command
from click import option
from click import Choice
@command() # command options
@option('--img', prompt='Image file') # image URI option
@option('--mode', type=Choice(['wallpaper', 'centered', 'scaled', 'stretched',
'zoom', 'spanned']), prompt='Display mode') # display mode option
def set_img(img, mode):
'''Set wallpaper image URI and display mode.'''
run(['gsettings', 'set', 'org.gnome.desktop.background',
'picture-options', mode], check=True) # set display mode
run(['gsettings', 'set', 'org.gnome.desktop.background',
'picture-uri', f'file:///home/foo/Pictures/{img}'], check=True) # set image URI
if __name__ == '__main__':
set_img() # if standalone
@nick3499
Copy link
Author

nick3499 commented May 24, 2020

User prompted for image file URI and display mode:

$ python3 set_bg_img.py 
Image file: foo.jpg
Display mode (wallpaper, centered, scaled, stretched, zoom, spanned): wallpaper	

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment