Set Windows Wallpaper fit/span/position using Python Script

594 views Asked by At

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?

1

There are 1 answers

2
Puddle On

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")