Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A How to programmatically get current wallpaper file path?

This answer will use Python 3 for code examples. I encourage people to post other answers for other languages. I'll continue to update this answer, as I'm able, based on comments with tested/verifi...

posted 2y ago by ghost-in-the-zsh‭  ·  edited 2y ago by ghost-in-the-zsh‭

Answer
#12: Post edited by user avatar ghost-in-the-zsh‭ · 2023-02-01T23:59:13Z (almost 2 years ago)
Modify emphasis
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • This is the reason the commands I had found hadn't been helpful, because I'm always using the dark theme, but the info/commands posted made no mention of these details.
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD?)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other languages**. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • This is the reason the commands I had found hadn't been helpful, because I'm always using the dark theme, but the info/commands posted made no mention of these details.
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD?)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
#11: Post edited by user avatar ghost-in-the-zsh‭ · 2023-02-01T19:48:58Z (almost 2 years ago)
Add note for gnome
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD?)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • This is the reason the commands I had found hadn't been helpful, because I'm always using the dark theme, but the info/commands posted made no mention of these details.
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD?)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
#10: Post edited by user avatar ghost-in-the-zsh‭ · 2023-02-01T11:05:56Z (almost 2 years ago)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD?)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
#9: Post edited by user avatar ghost-in-the-zsh‭ · 2023-02-01T11:04:56Z (almost 2 years ago)
Add sections for Windows 7 and 10
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • In Windows, there're at least two ways to go about this:
  • 1. Using a PowerShell command, such as `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper` via the `subprocess` module;
  • 2. Using the built-in [winreg](https://docs.python.org/3/library/winreg.html) module, which is part of the standard library (though only available in Windows).
  • I'm recommending Python's `winreg` for the following reasons:
  • 1. The implementation is more consistent across different versions of Windows;
  • 2. The PowerShell command `Get-ItemPropertyValue` is not implemented in Windows 7;
  • 3. It should perform better anyway, since it doesn't need to spawn a new process.
  • ### Windows 7/10
  • Code for Windows:
  • ```python
  • import winreg
  • hkey = winreg.HKEY_CURRENT_USER
  • subkey = 'Control Panel\\Desktop'
  • wallpaper_path = None
  • with winreg.OpenKeyEx(hkey, subkey) as regkey: # type: PyHKEY
  • value = winreg.QueryValueEx(regkey, 'Wallpaper') # type: Tuple[str, int]
  • wallpaper_path = value[0]
  • try:
  • with open(wallpaper_path): pass # see caveats below
  • except Exception:
  • # good luck
  • ```
  • The structure returned by the `QueryValueEx` function is a tuple that looks like this:
  • ```python
  • ('C:\\The\\Path To\\Your\\Wallpaper\\Image.jpg', 1)
  • ```
  • We're interested in the first tuple element, hence `value[0]`.
  • #### Caveats
  • The above code has the following *version-dependent* caveats:
  • 1. **Windows 7 (Python 3.7)**:
  • 1. Python 3.7 is the most recent version that will work in this Windows version, so this is how it was tested.
  • 2. When the user sets the wallpaper image, the OS seems to copy the file to a pre-determined location.
  • 3. This location seems to always remain valid, regardless of subsequent user actions.
  • 2. **Windows 10 (Python 3.11)**:
  • 1. When the user sets the wallpaper image, the OS simply stores the path to that image ***at the time it was set***. (The OS does not seem to copy the original image to any other location like Win7 does, which is bad for us.)
  • 2. Subsequent user actions on the original image (e.g. rename, delete, etc) ***will invalidate*** the registry value, which still points to the original location.
  • 3. This implies ***the path returned by `QueryValueEx` may not even exist*** at the time we query for it, which means you need a fallback.
  • 4. We use `open(wallpaper_path)` as a safe way to verify the path is valid and can be accessed.
  • 5. The PowerShell command returns the same path as `QueryValueEx`, which means the PowerShell code may be using the same function internally.
  • ### Windows 11 (TBD)
  • The code above *might* work, but I don't have Windows 11 and cannot test it here.
#8: Post edited by user avatar Alexei‭ · 2023-01-29T06:10:13Z (almost 2 years ago)
rolled back to previous version because there are issues running it in Python
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10
  • #### Registry
  • The full path to the image used to display the current wallpaper can be found in the `Registry -> HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper` ([source](https://superuser.com/a/1365985/530691), tested on Windows 10 Enterprise, version 20H2, OS Build 19042.2486, but I expect to also work on a home edition).
  • #### Powershell
  • This reads the registry key specified above. Powershell is preinstalled on all modern Windows editions.
  • ```powershell
  • Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper
  • ```
  • ### Windows 11
  • Similarly to Windows 10, the path can be read by:
  • - using **Registry Editor**: `HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper`
  • - using Powershell: `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper`
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
#7: Post edited by user avatar Alexei‭ · 2023-01-28T09:00:53Z (almost 2 years ago)
added info for Windows 11
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10
  • #### Registry
  • The full path to the image used to display the current wallpaper can be found in the `Registry -> HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper` ([source](https://superuser.com/a/1365985/530691), test on Windows 10 Enterprise, version 20H2, OS Build 19042.2486).
  • #### Powershell
  • This reads the registry key specified above.
  • ```powershell
  • Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper
  • ```
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10
  • #### Registry
  • The full path to the image used to display the current wallpaper can be found in the `Registry -> HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper` ([source](https://superuser.com/a/1365985/530691), tested on Windows 10 Enterprise, version 20H2, OS Build 19042.2486, but I expect to also work on a home edition).
  • #### Powershell
  • This reads the registry key specified above. Powershell is preinstalled on all modern Windows editions.
  • ```powershell
  • Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper
  • ```
  • ### Windows 11
  • Similarly to Windows 10, the path can be read by:
  • - using **Registry Editor**: `HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper`
  • - using Powershell: `Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper`
#6: Post edited by user avatar ghost-in-the-zsh‭ · 2023-01-28T01:25:30Z (almost 2 years ago)
Add note on GNU distro.
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10
  • #### Registry
  • The full path to the image used to display the current wallpaper can be found in the `Registry -> HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper` ([source](https://superuser.com/a/1365985/530691), test on Windows 10 Enterprise, version 20H2, OS Build 19042.2486).
  • #### Powershell
  • This reads the registry key specified above.
  • ```powershell
  • Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper
  • ```
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • **NOTE:** The GNU+Linux distro being used here is Ubuntu 22.10.
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10
  • #### Registry
  • The full path to the image used to display the current wallpaper can be found in the `Registry -> HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper` ([source](https://superuser.com/a/1365985/530691), test on Windows 10 Enterprise, version 20H2, OS Build 19042.2486).
  • #### Powershell
  • This reads the registry key specified above.
  • ```powershell
  • Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper
  • ```
  • ### Windows 11 (TBD)
#5: Post edited by user avatar Alexei‭ · 2023-01-27T11:07:46Z (almost 2 years ago)
added information about Windows 10
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10
  • #### Registry
  • The full path to the image used to display the current wallpaper can be found in the `Registry -> HKEY_CURRENT_USER\Control Panel\Desktop -> Wallpaper` ([source](https://superuser.com/a/1365985/530691), test on Windows 10 Enterprise, version 20H2, OS Build 19042.2486).
  • #### Powershell
  • This reads the registry key specified above.
  • ```powershell
  • Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Control Panel\Desktop" -Name Wallpaper
  • ```
  • ### Windows 11 (TBD)
#4: Post edited by user avatar ghost-in-the-zsh‭ · 2023-01-26T01:38:34Z (almost 2 years ago)
Add Gnome section
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome (TBD)
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome
  • The process under Gnome boils down to the following steps:
  • 1. Find out what the user's theme is, and
  • 2. Use the theme-specific command to get the wallpaper path.
  • #### Theme
  • On a clean installation (e.g. Ubuntu 22.10), the user has the following theme options:
  • 1. Default
  • 2. Dark
  • Use this command to find out the theme currently set by the user:
  • ```bash
  • gsettings get org.gnome.desktop.interface color-scheme
  • ```
  • The possible results are below:
  • | Theme | Value |
  • | :---- | :---- |
  • | Default | `'default'` |
  • | Dark | `'prefer-dark'` |
  • You can use the `subprocess` module for this:
  • ```python
  • theme = subprocess.run(
  • 'gsettings get org.gnome.desktop.interface color-scheme',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '')
  • ```
  • The returned string must have newlines and extra `'` characters removed, hence the `strip` and `replace` calls.
  • #### Path
  • The command to get the wallpaper path is:
  • ```bash
  • gsettings get org.gnome.desktop.background <key>
  • ```
  • where `<key>` is one of the following *theme-dependent* values:
  • | Theme | Key |
  • | :---- | :---- |
  • | `default` | `picture-uri` |
  • | `prefer-dark` | `picture-uri-dark` |
  • **NOTE:** Correct key selection depends on the result from the previous section. Using the wrong key will still give you a file system path. However, that path is not necessarily the current wallpaper (you could get lucky). In fact, the path may not even point to an existing file (e.g. running the command under KDE).
  • For example, if the user is using the Dark theme, then the correct command to get the path is:
  • ```bash
  • gsettings get org.gnome.desktop.background picture-uri-dark
  • ```
  • Assuming the `theme` variable from the previous section, you can, again, use the `subprocess` module:
  • ```python
  • theme_key = 'picture-uri-dark' if theme == 'prefer-dark' else 'picture-uri'
  • wallpaper_path = subprocess.run(
  • f'gsettings get org.gnome.desktop.background {theme_key}',
  • capture_output=True,
  • shell=True,
  • universal_newlines=True
  • ).stdout.strip().replace("'", '').removeprefix('file://')
  • ```
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
#3: Post edited by user avatar ghost-in-the-zsh‭ · 2023-01-25T00:20:20Z (almost 2 years ago)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for other languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome (TBD)
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for **other** languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome (TBD)
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
#2: Post edited by user avatar ghost-in-the-zsh‭ · 2023-01-25T00:19:45Z (almost 2 years ago)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for other languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome (TBD)
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
  • This answer will use Python 3 for code examples. I encourage people to post other answers for other languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc.
  • # Checking OS
  • ```python
  • import platform
  • sys = platform.system().lower()
  • if sys == 'linux':
  • # do Linux stuff here
  • elif sys == 'windows':
  • # do Windows stuff here
  • else:
  • # ...
  • ```
  • ## GNU+Linux
  • To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:
  • 1. KDE/Plasma: `plasma`
  • 2. Gnome: `ubuntu`
  • 3. Unity: **(TBD)**
  • If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.
  • Code:
  • ```python
  • from os import environ as env
  • kde = ('plasma',)
  • gnome = ('ubuntu',)
  • session = env['DESKTOP_SESSION']
  • if session in kde:
  • # KDE/Plasma implementation here
  • elif session in gnome:
  • # Gnome implementation here
  • else:
  • # ...
  • ```
  • ### 1. KDE/Plasma
  • Find the following file/line:
  • 1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
  • 2. **Line:** `Image=file://<abs-path>`
  • Note that:
  • 1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
  • 2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
  • 3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
  • 4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.
  • Code:
  • ```python
  • from os.path import join, expanduser
  • wallpaper_path = None
  • configpath = join(
  • expanduser('~'),
  • '.config',
  • 'plasma-org.kde.plasma.desktop-appletsrc'
  • )
  • with open(configpath) as config:
  • for line in config.readlines():
  • if line.startswith('Image='):
  • wallpaper_path = line.split('=')[1].removeprefix('file://').strip()
  • break
  • ```
  • ### 2. Gnome (TBD)
  • ### 3. Unity (TBD)
  • ## Microsoft
  • ### Windows 7 (TBD)
  • ### Windows 10 (TBD)
  • ### Windows 11 (TBD)
#1: Initial revision by user avatar ghost-in-the-zsh‭ · 2023-01-25T00:17:35Z (almost 2 years ago)
This answer will use Python 3 for code examples. I encourage people to post other answers for other languages. I'll continue to update this answer, as I'm able, based on comments with tested/verified recommendations/code/etc. 

# Checking OS

```python
import platform

sys = platform.system().lower()
if sys == 'linux':
    # do Linux stuff here
elif sys == 'windows':
    # do Windows stuff here
else:
    # ...
```

## GNU+Linux

To identify which desktop environment is being used, I'm using the `DESKTOP_SESSION` environment variable. I get the following string values:

1. KDE/Plasma: `plasma`
2. Gnome: `ubuntu`
3. Unity: **(TBD)**

If there're other synonyms and/or strings for different versions of the same environment, that'd also be good to know so it can be added above.

Code:

```python
from os import environ as env

kde = ('plasma',)
gnome = ('ubuntu',)
session = env['DESKTOP_SESSION']

if session in kde:
    # KDE/Plasma implementation here
elif session in gnome:
    # Gnome implementation here
```

### 1. KDE/Plasma

Find the following file/line:

1. **File:** `~/.config/plasma-org.kde.plasma.desktop-appletsrc`
2. **Line:** `Image=file://<abs-path>`

Note that:

1. `<abs-path>` is the absolute path to the file in the file system (e.g. `/home/<you>/Pictures/your-wallpaper.jpg`);
2. you may have multiple `Image=` entries (e.g. due to multiple plasma activities; I have 2 activities and 2 entries; entry 1 is for activity 1, etc.);
3. we're only grabbing the 1st entry, which is the wallpaper for the 1st activity;
4. you can look at the `kscreenlockerrc` file if interested in the image used by the screen locker.

Code:

```python
from os.path import join, expanduser

wallpaper = None
configpath = join(
    expanduser('~'),
    '.config',
    'plasma-org.kde.plasma.desktop-appletsrc'
)
with open(configpath) as config:
    for line in config.readlines():
        if line.startswith('Image='):
            wallpaper = line.split('=')[1].removeprefix('file://').strip()
            break
```

### 2. Gnome (TBD)

### 3. Unity (TBD)

## Microsoft

### Windows 7 (TBD)

### Windows 10 (TBD)

### Windows 11 (TBD)