Python,从 URL 更改桌面背景

Python, Changing Desktop Background from URL

我目前有这个:

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)

Updated Answer: some URLs might return HTTP Error 403: Forbidden.

试试这个:

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)