window下一键更换bing每日壁纸

环境

python3.6
win10

bing接口

Url: https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1
    
Method: GET
    
Response:
{
    "images": [{
        "startdate": "20190424",
        "fullstartdate": "201904241600",
        "enddate": "20190425",
        "url": "/th?id=OHR.FireIce_ZH-CN2924097132_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
        "urlbase": "/th?id=OHR.FireIce_ZH-CN2924097132",
        "copyright": "日落时瓦特纳冰川上的冰洞,冰岛 (© Johnathan Ampersand Esper/Aurora Photos)",
        "copyrightlink": "http://www.bing.com/search?q=%E5%86%B0%E6%B4%9E&form=hpcapt&mkt=zh-cn",
        "title": "",
        "quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20190424_FireIce%22&FORM=HPQUIZ",
        "wp": true,
        "hsh": "5583b6cd086a3c48837d10d800cbfaa0",
        "drk": 1,
        "top": 1,
        "bot": 1,
        "hs": []
    }],
    "tooltips": {
        "loading": "正在加载...",
        "previous": "上一个图像",
        "next": "下一个图像",
        "walle": "此图片不能下载用作壁纸。",
        "walls": "下载今日美图。仅限用作桌面壁纸。"
    }
}

更换壁纸代码

import requests
import win32gui
import os
from PIL import Image
from io import BytesIO

baseUrl = 'https://cn.bing.com/'
apiArgs = 'HPImageArchive.aspx?format=js&idx=0&n=1'
imgPath = r"D://wall.bmp"
try:
    response = requests.get(baseUrl + apiArgs)
    api = response.json()
    imageUrl = baseUrl + api.get('images')[0].get('url')
    r = requests.get(imageUrl)
    i = Image.open(BytesIO(r.content))
    i.save(imgPath,"bmp")
    win32gui.SystemParametersInfo(20, imgPath, 3)
except Exception as e:
    pass
finally:
    if os.path.exists(imgPath):
        os.remove(imgPath)

打包成exe文件

pip install pyinstaller cryptography
pyinstaller --onefile --nowindowed --icon="C:\Users\95250\Desktop\wall.ico" wallpaper.py

你可能感兴趣的:(window下一键更换bing每日壁纸)