1

I've been tring to change the desktop background using Python 2.7 :

SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pngName, 0)

( While pngName is a valid path to an image )

When I run the script nothing changes, and when I try it with a different image it works just fine.

Any suggestions?

p.s. can someone please explain how that script works? I've taken it from somewhere and don't quite understand it.

Thanks!

Ben L
  • 743
  • 2
  • 7
  • 15

1 Answers1

1

Instead of using png files (I believe you're using them, given the variable name for image paths is pngName), try jpg. It seems that MS Windows doesn't support png as a background image format (As a Linux/Mac user, I cannot confirm it myself, but see this discussion for more details).

Regarding how your script works, I can briefly say that it uses ctypes Python package that allows you to call functions in dlls/shared libraries such as SystemParametersInfo. SystemParametersInfo (see MSDN page) retrieves/sets values of some system-wide parameters, including setting Desktop parameters like wallpaper file. See Example Three here for more insights.

vrs
  • 1,922
  • 16
  • 23
  • I appreciate your answer. Tried using both png and jpg with no luck. If i print : ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pngName, 0) I get 0, which indicates it can't find the image if I'm not mistaken. However I triple-checked and found nothing. Any other suggestion? And thank you for the scond part. Very helpful Edit: Is it possible that it happens becuase the picture is a paint file saved as jpg? because an original photo from the same dir actually worked. – Ben L Dec 15 '15 at 19:28
  • 1
    @BenL hmm, then I'd suggest to follow the discussion that I mentioned above (take a look at the first answer at least). Some people report that resizing jpg to match your screen resolution and dpi solves the problem. SystemParametersInfoA returns boolean, and 0 means it fails to set the wallpaper. To get a more specific error message, you need to use [ctypes.GetLastError()](https://docs.python.org/2/library/ctypes.html) (see also [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360%28v=vs.85%29.aspx)) – vrs Dec 16 '15 at 11:01