Set Windows Wallpaper fit/span/position using Python Script
Asked Answered
L

1

2

There are many questions and articles about how to change desktop wallpaper using python script. Which is as under:

file = "---path---"
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, file , 0)

However, this code doesn't change the wallpaper position. It only changes the wallpaper file. Say, the earlier wallpaper was positioned as "fill". Then the new wallpaper will also be set as "fill". And we have to manually change that position to fit/fill/span/tile/centered as per our requirement.

There's a documentation here which talks about changing the wallpaper position using a C++ function using DESKTOP_WALLPAPER_POSITION enumeration (shobjidl_core.h).

However, I am not able to use that in python.

Can someone help me and point me in the right direction?

Can we change the wallpaper fit position using python script? How do we pass that parameter to the above code?

Leafy answered 27/1, 2021 at 7:17 Comment(0)
E
0

You simply have to modify 2 registry values. And need to set the wallpaper so it takes effect.

import winreg, ctypes, win32con

FILL,FIT,STRETCH,TILE,CENTER,SPAN = 0,1,2,3,4,5
MODES = (0,10),(0,6),(0,2),(1,0),(0,0),(0,22)
value1,value2 = MODES[FILL] # choose mode here

key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Control Panel\Desktop", 0, winreg.KEY_WRITE)
winreg.SetValueEx(key, "TileWallpaper", 0, winreg.REG_SZ, str(value1))
winreg.SetValueEx(key, "WallpaperStyle", 0, winreg.REG_SZ, str(value2))
winreg.CloseKey(key)

def setWallpaper(path):
    changed = win32con.SPIF_UPDATEINIFILE | win32con.SPIF_SENDCHANGE
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_SETDESKWALLPAPER,0,path,changed)

setWallpaper("C:/absolute/path/to/your/image.jpg")
Exponential answered 7/4, 2022 at 15:30 Comment(2)
Hey. Yeah that can work. But i need a solution that is similar to how we set wallpeper using mentioned python code. Don't want to use registry. Any way to do that using system parameters?Leafy
@Meet The only other way i know is to use a dll. See this answer. Although they're just using SetWallpaper not SetPosition. Also see this github for a link to a dll. I think just changing the registry yourself is the simplest solution so far. And if anyone has made a lib, it's probably what they're doing underneath.Exponential

© 2022 - 2024 — McMap. All rights reserved.