使用Python腳本將Bing的每日圖片作為桌面的教程
微軟最近出了個 必應bing 繽紛桌面,使用下來還是不錯,可以每天更換Bing首頁的北京作為壁紙,但是該軟件有個不好的地方是,安裝后桌面上會有一個搜索框出現(xiàn),很是煩人,而且不能關(guān)掉。于是出于技術(shù)考慮,想到了使用Python來實現(xiàn)這個功能。
正如很多介紹Python書中那樣,Python是中膠水語言,用在哪里都是可行的。想要使用Python給桌面設置背景只需要下個模塊安裝即可:
http://sourceforge.net/projects/pywin32/
代碼非常簡單,參考了網(wǎng)上一些其他人寫了代碼,具體代碼如下:
# -*- coding: utf-8 -*-
import urllib,time,os,Image,win32gui,win32con,win32api
class StealBing:
def __init__(self):
self.content = urllib.urlopen('http://cn.bing.com/').read()
self.bgImageUrl = ''
self.localFileName = ''
self.localBMPFileName = ''
def parserImageURL(self):
tempStr = self.content[self.content.index('g_img={url:')+12:]
self.bgImageUrl = tempStr[:tempStr.index('id:\'bgDiv\'')-2]
def createLocalFileName(self):
randomStr = time.strftime("%Y%m%d", time.localtime())
self.localFileName = 'D:/Bing/' + randomStr + '.jpg'
self.localBMPFileName = 'D:/Bing/' + randomStr + '.bmp'
def downloadImage(self):
if self.bgImageUrl == '':
self.parserImageURL()
if self.localFileName == '':
self.createLocalFileName()
urllib.urlretrieve(self.bgImageUrl, self.localFileName)
def updateBGImage(self):
img = Image.open(self.localFileName)
img.save(self.localBMPFileName)
os.remove(self.localFileName)
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸適應桌面,0桌面居中
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, self.localBMPFileName , 1+2)
if __name__ == '__main__':
stealBing = StealBing()
stealBing.downloadImage()
stealBing.updateBGImage()
相關(guān)文章
Python趣味挑戰(zhàn)之教你用pygame畫進度條
pygame四種方法教會你畫進度條,其實也不難,文中有非常詳細的代碼示例,對正在學習python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Django中從mysql數(shù)據(jù)庫中獲取數(shù)據(jù)傳到echarts方式
這篇文章主要介紹了Django中從mysql數(shù)據(jù)庫中獲取數(shù)據(jù)傳到echarts方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python torch.utils.data.DataLoader使用方法
這篇文章主要介紹了python torch.utils.data.DataLoader使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Python批量實現(xiàn)橫屏轉(zhuǎn)豎屏的視頻處理工具
這篇文章主要為大家詳細介紹了如何使用Python和Tkinter框架開發(fā)一個視頻處理器應用,用于批量橫屏轉(zhuǎn)豎屏視頻處理,支持多種視頻格式和編碼選擇,需要的可以了解下2025-02-02

