Python,从URL更改桌面背景

Python,从URL更改桌面背景,python,background,desktop,Python,Background,Desktop,我目前有: import ctypes PATH = 'F:\Designs\Privat\Random Banner\Banner.png' ctypes.windll.user32.SystemParametersInfoW(20, 0, PATH, 3) 但是现在,我想从URL获取图像,我如何才能做到这一点?您可以尝试以下方法: import urllib.request import ctypes URL = "https://www.google.gr/images/b

我目前有:

import ctypes

PATH = 'F:\Designs\Privat\Random Banner\Banner.png'
ctypes.windll.user32.SystemParametersInfoW(20, 0, PATH, 3)
但是现在,我想从URL获取图像,我如何才能做到这一点?

您可以尝试以下方法:

import urllib.request
import ctypes

URL = "https://www.google.gr/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"

PATH=urllib.request.urlretrieve(URL)[0]

ctypes.windll.user32.SystemParametersInfoW(20, 0, PATH, 3)
更新的答案:某些URL可能返回
HTTP错误403:禁止

请尝试以下方法:

import os
import requests
import ctypes

url = 'https://cdn.discordapp.com/attachments/692713673076113461/741287055610609684/hqdefault.png'
r = requests.get(url)
name = "background_image.png"

file = open(name, "wb")
file.write(r.content)
file.close()
PATH = os.path.abspath(name)

ctypes.windll.user32.SystemParametersInfoW(20, 0, PATH, 3)
您可以尝试以下方法:

import urllib.request
import ctypes

URL = "https://www.google.gr/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"

PATH=urllib.request.urlretrieve(URL)[0]

ctypes.windll.user32.SystemParametersInfoW(20, 0, PATH, 3)
更新的答案:某些URL可能返回
HTTP错误403:禁止

请尝试以下方法:

import os
import requests
import ctypes

url = 'https://cdn.discordapp.com/attachments/692713673076113461/741287055610609684/hqdefault.png'
r = requests.get(url)
name = "background_image.png"

file = open(name, "wb")
file.write(r.content)
file.close()
PATH = os.path.abspath(name)

ctypes.windll.user32.SystemParametersInfoW(20, 0, PATH, 3)

我只能用谷歌图片做这个,对吗?因为当我使用来自discord或其他网站的图像时,我会收到一个错误嗯,你能发送链接或类似的链接吗?如果您需要身份验证才能到达那里,那么现在可能需要身份验证work@DerNoob请尝试我编辑过的答案。更新的部分。我只能用谷歌图片做这个,对吗?因为当我使用来自discord或其他网站的图像时,我会收到一个错误嗯,你能发送链接或类似的链接吗?如果您需要身份验证才能到达那里,那么现在可能需要身份验证work@DerNoob请尝试我编辑过的答案。更新的部分。