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.

Michael Bell
  • 184
  • 1
  • 2
  • 16
  • Could it be that the wallpaper setting is set to dynamic. Changes wallpapers after some time. – user568109 Jun 05 '13 at 15:29
  • Hmmm. I ran the python script again, it changes the background image, but the image selected is still the old image... – Michael Bell Jun 05 '13 at 15:32
  • 2
    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`. – Eryk Sun Jun 05 '13 at 19:31
  • 1
    @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. – Michael Bell Jun 05 '13 at 21:16
  • 1
    Are using an absolute path? "myimage.jpg" probably works at first because it's relative to the current working directory of your process. – Eryk Sun Jun 05 '13 at 21:38
  • Now I can't change my background off of a black screen 0.o – Michael Bell Jun 06 '13 at 00:36
  • 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](http://www.ehow.com/how_10025846_remove-transcoded-wallpaper.html) 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. – Eryk Sun Jun 06 '13 at 01:30
  • possible duplicate of [change desktop background](http://stackoverflow.com/questions/1977694/change-desktop-background) – bernard paulus Nov 21 '14 at 15:07
  • I provided answer with example for similar SO question. [change desktop background](http://stackoverflow.com/questions/1977694/change-desktop-background/37669111#37669111) – Vlad Bezden Jun 07 '16 at 13:37

1 Answers1

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)

Dan O'Boyle
  • 3,676
  • 5
  • 28
  • 44
  • Hi. Can you help with this? https://stackoverflow.com/questions/65914485/set-windows-wallpaper-fit-span-position-using-python-script – Meet Apr 07 '22 at 04:25