Home >Backend Development >Python Tutorial >Use Python to download wallpapers and automatically change the desktop
We use an open source Bing Wallpaper API on GitHub as the source of wallpaper
https://github. com/zenghongtu/bing-wallpaper
From the readme we can know that in the web application I only need to use the following reference
<img src="https://img.php.cn/"/ alt="Use Python to download wallpapers and automatically change the desktop" >
It’s so convenient
Let’s take a look at the specific calling rules of the API
1. Incoming The resolution parameter specifies the resolution of the wallpaper image. The default is 1920x1080, the optional values are as follows:
UHD 1920x1200 1920x1080 1366x768 1280x768 1024x768 800x600 800x480 768x1280 720x1280 640x480 480x800 400x240 320x240 240x320
UHD means high definition, and the picture is larger.
2. Pass in index to get pictures of a certain day, 0 means today, 1 means yesterday, and so on, index=random means a random day.
3. Pass in date to get pictures from a certain day to today, such as data=20210401.
4. Pass in w and h to specify the width and height of the image.
5. Pass in qlt to specify the quality of the image, the value range is 0 to 100.
For example
We enter the following address directly into the browser
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" }
It can be said to be quite convenient
You can also use it directly in css
background-image: url(https://bingw.jasonzeng.dev/?index=random); height: 100%; background-position: center; background-repeat: no-repeat; background-size: cover;
Let’s take a look at how to call it through Python, it is also very simple
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()
The above code is to get the first 30 pictures For wallpapers, we can modify the range parameters to obtain different numbers of wallpapers
The capture effect is as follows:
Wallpaper With that, let’s automatically switch the desktop wallpaper. Here we use win32con and win32gui to operate the desktop wallpaper
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) # 刷新桌面
and then select the picture from the downloaded wallpaper
def change_wallpaper(): pic_list = os.listdir("wallpaper")# 得到文件路径下的Use Python to download wallpapers and automatically change the desktop,列表类型 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):# 如果是最后一张Use Python to download wallpapers and automatically change the desktop,则重新到第一张 i=0 if __name__ == '__main__': change_wallpaper()
Such a simple automatic The tool for switching desktop wallpaper is complete, come and try it!
The above is the detailed content of Use Python to download wallpapers and automatically change the desktop. For more information, please follow other related articles on the PHP Chinese website!