ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

使用Python获取当前Bing的背景图片并设置为Windows壁纸

2022-03-28 08:31:26  阅读:183  来源: 互联网

标签:__ Python res Bing jpg fileName Windows 壁纸 import


下面的代码在我写此博客时编辑通过,很简单,如果Bing的背景图片格式发生变化,请修改对应的正则匹配代码即可。

 

#-------------------------------------------------------------------------------
# Name:        BingWallPaper
# Purpose:
#
# Author:      xxh
#
# Created:     28-03-2022
# Copyright:   (c) xxh 2022
# Licence:     <your licence>
#-------------------------------------------------------------------------------

import requests
import re
import win32gui
import os
import datetime,time
from PIL import Image


# 设置壁纸
def setWallpaper(imgPath):
    win32gui.SystemParametersInfo(20, imgPath, 3)

# 下载图片
def downImg(IMAGE_URL):
    # 图片以当天日期命名,这里默认为当前路径,实际使用时可以指定一个壁纸存放的专门路径
    fileName = time.strftime("%Y-%m-%d", time.localtime()) +'.jpg'
    r = requests.get(IMAGE_URL)
    with open(fileName, 'wb') as f:
        f.write(r.content)
    return fileName

def main():
    res = requests.get('https://cn.bing.com/')
    res.encoding = 'utf-8'
    # res.text为网页源码,这里要用到的是图片
    # <link rel="preload" href="https://s.cn.bing.net/th?id=OHR.Kawachi_ZH-CN6964965791_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg" as="image" id="preloadBg"  />
    pattern = re.compile(r'href="(https://s\.cn\.bing\.net/th\?id=.*\.jpg)" as=')
    result =  pattern.findall(res.text)
    # result[0]:https://s.cn.bing.net/th?id=OHR.Kawachi_ZH-CN6964965791_1920x1080.jpg&amp;rf=LaDigue_1920x1080.jpg
    fileName = downImg(result[0])
    # 转换文件格式为bmp
    im = Image.open(fileName)
    im.convert('RGB').save(fileName[:-3]+'bmp','BMP')
    # 设置壁纸
    setWallpaper(os.getcwd()+'/'+fileName[:-3]+'bmp')


if __name__ == '__main__':
    main()

 

标签:__,Python,res,Bing,jpg,fileName,Windows,壁纸,import
来源: https://www.cnblogs.com/GarfieldTom/p/16065498.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有