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?

Meet
  • 461
  • 4
  • 19

1 Answers1

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")
Puddle
  • 2,993
  • 1
  • 19
  • 32
  • 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? – Meet Apr 08 '22 at 17:40
  • 1
    @Meet The only other way i know is to use a dll. See this [answer](https://stackoverflow.com/questions/66375014/is-it-possible-to-use-idesktopwallpaper-in-python). Although they're just using `SetWallpaper` not `SetPosition`. Also see this [github](https://github.com/xiongnemo/windows-wallpaper-shuffler) 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. – Puddle Apr 09 '22 at 04:01