用python当壁纸_使用Python脚本将Bing的每日图片作为桌面的教程

这篇博客介绍了如何使用Python脚本来实现从Bing下载每日壁纸,并将其设置为Windows桌面背景。作者通过解析Bing网页源代码获取图片URL,然后下载图片并转换格式,最后利用Windows API设置壁纸。这个脚本解决了第三方软件中不必要桌面搜索框的问题。
摘要由CSDN通过智能技术生成

微软最近出了个 必应bing 缤纷桌面,使用下来还是不错,可以每天更换Bing首页的北京作为壁纸,但是该软件有个不好的地方是,安装后桌面上会有一个搜索框出现,很是烦人,而且不能关掉。于是出于技术考虑,想到了使用Python来实现这个功能。

正如很多介绍Python书中那样,Python是中胶水语言,用在哪里都是可行的。想要使用Python给桌面设置背景只需要下个模块安装即可:

http://sourceforge.net/projects/pywin32/

代码非常简单,参考了网上一些其他人写了代码,具体代码如下:

# -*- 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值