今天我们继续用Python写一个小工具,一方面实现壁纸自由,另一方面实现桌面更换自由!
成都创新互联公司专注于水磨沟网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供水磨沟营销型网站建设,水磨沟网站制作、水磨沟网页设计、水磨沟网站官网定制、重庆小程序开发服务,打造水磨沟网络公司原创品牌,更为您提供水磨沟网站排名全网营销落地服务。
我们这里使用一个开源在 GitHub 上的必应壁纸 API 作为壁纸的来源
https://github.com/zenghongtu/bing-wallpaper
图片
从 readme 当中我们可以知道,在 web 应用中我只需要使用如下引用即可

实在是太方便了
下面我们来看下该 API 的具体调用规则
UHD
1920x1200
1920x1080
1366x768
1280x768
1024x768
800x600
800x480
768x1280
720x1280
640x480
480x800
400x240
320x240
240x320
UHD 就是高清,图片比较大。
我们直接在浏览器输入如下地址
http://bingw.jasonzeng.dev?resolutinotallow=UHD&index=random&w=1000&format=json
Output:
{
"startdate": "20220105",
"copyright": "Plate-billed mountain toucan in Bellavista Cloud Forest Reserve, Ecuador (© Tui De Roy/Minden Pictures)",
"urlbase": "/th?id=OHR.MountainToucan_EN-US7120632569",
"title": "A plate-billed mountain toucan",
"url": "https://www.bing.com/th?id=OHR.MountainToucan_EN-US7120632569_UHD.jpg&w=1000"
}
可以说是相当方便了
也可以直接在 css 当中使用
background-image: url(https://bingw.jasonzeng.dev/?index=random);
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
下面我们看一下如何通过 Python 进行调用,也很简单
import requests
def get_wallpaper():
for i in range(30):
url = "https://bingw.jasonzeng.dev?resolutinotallow=UHD&index=%s" % str(i)
print(url)
res = requests.get(url)
with open("wallpaper/" + "%s.jpg" % str(i),"wb") as w:
w.write(res.content)
if __name__ == "__main__":
get_wallpaper()
上面代码就是获取前30张壁纸,我们可以修改range的参数,来获取不同数量的壁纸
抓取效果如下
图片
壁纸有了,下面我们就来进行自动切换桌面壁纸,这里使用win32con和win32gui操作桌面壁纸
def windows_img(paper_path):
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control panel\\Desktop", 0, win32con.KEY_SET_VALUE) # 在注册表中写入属性值
win32api.RegSetValueEx(k, "wapaperStyle", 0, win32con.REG_SZ,"2") # 0 代表桌面居中 2 代表拉伸桌面
win32api.RegSetValueEx(k, "Tilewallpaper", 0, win32con.REG_SZ,"0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, paper_path, win32con.SPIF_SENDWININICHANGE) # 刷新桌面
然后就是从已经下载的壁纸当中选择图片
def change_wallpaper():
pic_list = os.listdir("wallpaper") # 得到文件路径下的图片,列表类型
i=0
print(pic_list)
while True:
pic = "wallpaper"+'\{}'.format(pic_list[i])
abspath_pic = os.path.abspath(pic)
windows_img(abspath_pic)
print(abspath_pic)
time.sleep(1000) # 设置壁纸更换间隔
i += 1
if i==len(pic_list): # 如果是最后一张图片,则重新到第一张
i=0
if __name__ == '__main__':
change_wallpaper()
这样一个简单的自动切换桌面壁纸的工具就完成了,快来尝试一下吧!
分享题目:用Python下载壁纸并自动更换桌面
网页链接:http://www.csdahua.cn/qtweb/news49/298649.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网