Change Windows Background from Python
Asked Answered
S

1

8

Does anyone know a way to change the Windows Desktop Wallpaper with python so that the change is permanent? I have found this code

import ctypes
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "myimage.jpg" , 0)

This code works, but once you log off and log on again, the background is back to the original image. I would prefer a solution that does not require any registry edit, and I would like something that works with Windows XP and 7 if it is possible.

Sankhya answered 5/6, 2013 at 15:20 Comment(9)
Could it be that the wallpaper setting is set to dynamic. Changes wallpapers after some time.Phenomena
Hmmm. I ran the python script again, it changes the background image, but the image selected is still the old image...Sankhya
The last parameter, fWinIni, "specifies whether the user profile is to be updated". The flags are SPIF_UPDATEINIFILE == 1 and SPIF_SENDCHANGE == 2. The latter broadcasts a WM_SETTINGCHANGE message. Try using fWinIni == 3.Medora
@eryksun changing it to 3 causes my background to become black on the next login. The Desktop background window shows that my background is now a file called myimage which is a black window.Sankhya
Are using an absolute path? "myimage.jpg" probably works at first because it's relative to the current working directory of your process.Medora
Now I can't change my background off of a black screen 0.oSankhya
The way you're setting it is the preferred way (assuming you use an absolute path for the image), but maybe some setting got corrupted. Search for step-by-step instructions to reset the wallpaper. I think Windows 7 "Starter Edition" doesn't support changing the background (that's ridiculous), and on XP you'll have to first transcode a JPG to BMP.Medora
possible duplicate of change desktop backgroundTeetotalism
I provided answer with example for similar SO question. change desktop backgroundDerward
A
4

This solution combines several of the comments made, and works for me:

import ctypes
import os
drive = "C:\\"
folder = "images"
image = "test.jpg"
image_path = os.path.join(drive, folder, image)
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image_path, 3)

(Note that you should determine the absolute path to your image, and change as needed. Also convert the image to BMP if you need to use it on XP. You can easily convert the image using Pillow)

Ambience answered 7/6, 2015 at 18:49 Comment(1)
Hi. Can you help with this? #65914985Twiddle

© 2022 - 2024 — McMap. All rights reserved.