为什么我的桌面屏幕变黑?
Why does my desktop screen go black?
我正在尝试将图像设为我的桌面图像。
我的图片路径:
#Image Path
import random,os
folder= r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca[=10=].91.6_0\backgrounds"
a=random.choice(os.listdir(folder))
print(a)
打开图片:
#Opening the Image
from PIL import Image
file = folder+'\'+a
Image.open(file).show()
将图像设为我的桌面图像:
#Making the image as desktop image
import ctypes
image_path = file
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image_path, 3)
现在问题是找到了图像路径,它还会打开一个 image.But 而不是将打开的图像设置为桌面图像,它只会使我的桌面图像变黑。
这样做就可以了:
import random,os
import ctypes
folder= r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca[=10=].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)
您的文件夹变量中有一个“\”到多个
这篇文章: 我认为可以解释这个问题。
似乎 Win32Api 是为 C 编写的,因此需要特定格式的字符串 - 空终止字符数组。
不完全确定如何在 Python 中传递这样的数据,但希望这会为您指明正确的方向
不是它变黑了,而是你的背景变纯了。转到背景设置,将背景设置为黑色以外的纯色。然后,将背景更改为图片。现在,运行 你的代码。您会看到问题是它变成了纯色,而不是黑色图像。
下一个解决方案也可以提供帮助:
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)
我正在尝试将图像设为我的桌面图像。
我的图片路径:
#Image Path
import random,os
folder= r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca[=10=].91.6_0\backgrounds"
a=random.choice(os.listdir(folder))
print(a)
打开图片:
#Opening the Image
from PIL import Image
file = folder+'\'+a
Image.open(file).show()
将图像设为我的桌面图像:
#Making the image as desktop image
import ctypes
image_path = file
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image_path, 3)
现在问题是找到了图像路径,它还会打开一个 image.But 而不是将打开的图像设置为桌面图像,它只会使我的桌面图像变黑。
这样做就可以了:
import random,os
import ctypes
folder= r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca[=10=].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)
您的文件夹变量中有一个“\”到多个
这篇文章:
不完全确定如何在 Python 中传递这样的数据,但希望这会为您指明正确的方向
不是它变黑了,而是你的背景变纯了。转到背景设置,将背景设置为黑色以外的纯色。然后,将背景更改为图片。现在,运行 你的代码。您会看到问题是它变成了纯色,而不是黑色图像。
下一个解决方案也可以提供帮助:
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)