1

I am trying to make a image my desktop image.
My Image path:

#Image Path
import random,os
folder= r"C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"
a=random.choice(os.listdir(folder))
print(a)

Opening the image:

#Opening the Image
from PIL import Image
file = folder+'\\'+a
Image.open(file).show()

Making the image as my desktop image:

#Making the image as desktop image
import ctypes
image_path = file
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image_path, 3)

Now the thing is the image path is found and it also opens an image.But instead of setting the opened image as desktop image, it just make my desktop image black.

White Shadow
  • 444
  • 2
  • 10
  • 26

4 Answers4

2

This wil do the trick:

import random,os
import ctypes 

folder= r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"
a=random.choice(os.listdir(folder)) 
print(a) file = folder+ '\\' +a
SPI_SETDESKWALLPAPER = 20 
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER , 0, file, 3)

You had one "\" to many in your folder variable

Stan Vanhoorn
  • 526
  • 1
  • 4
  • 18
0

This article: SystemParametersInfo sets wallpaper completly black (using SPI_SETDESKWALLPAPER) I think explains the issue. Seems the Win32Api is written for C, and so expects strings in a particular format - a null terminated char array.

Not entirely sure how to pass data like this in Python, but hopefully this will point you in the right direction

Community
  • 1
  • 1
cyberspy
  • 1,023
  • 11
  • 13
0

It's not that it goes black, it's that your background turns solid. Go to your background settings, set your background to a solid color other than black. Afterwards, change your background to a picture. Now, run your code. You will see that the issue is that it changes to a solid color, not a black image.

MmBaguette
  • 340
  • 3
  • 13
0

Also next solution can help:

import os
import ctypes

folder = r"C:\folder"
file_name = r"back.jpg"

full_path = os.path.join(folder, file_name)
wallpaper = bytes(full_path, 'utf-8')

ctypes.windll.user32.SystemParametersInfoA(20, 0, wallpaper, 3)
Vetedde
  • 165
  • 7