用Python设置桌面墙纸

网上看的一段比较好玩的代码, 利用Python设置windows系统的桌面.

原文 http://seasons.javaeye.com/blog/60294

This is a python snippet to set wallpaper as the photo of the day in national geographic.

1. install python form http://www.python.org/download/
2. install python image library(PLI) from http://www.pythonware.com/products/pil/
3. say the code below and run it by python


python 代码

"""
Set the wallpaper as the picture got from national geographic picture of the day
by
seasons@gmail.com
"""
import ctypes
import Image
import calendar
import urllib
import socket

STOREDIR = 'C:/lg_wallpaper/pod/'

def setWallpaperFromBMP(imagepath):
SPI_SETDESKWALLPAPER = 20 # According to
http://support.microsoft.com/default.aspx?scid=97142
#注意这条语句

ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagepath , 0) #SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE

def setWallPaper(imagePath):
"""Given a path to an image, convert it to bmp and set it as wallpaper"""
bmpImage = Image.open(imagePath)
newPath = STOREDIR + 'mywallpaper.bmp'
bmpImage.save(newPath, "BMP")
setWallpaperFromBMP(newPath)

def getPicture(fname):
sock = urllib.urlopen("
http://lava.nationalgeographic.com/pod/index.html")
htmlSource = sock.read()
sock.close()
pos1 = htmlSource.find('sm_wallpaper')
pos2 = htmlSource.find('/pod/pictures/normal')
filename = htmlSource[pos1+13:pos2-12]
fileurl = 'http://lava.nationalgeographic.com/pod/pictures/lg_wallpaper/'+filename
urllib.urlretrieve(fileurl, fname)
print 'got the picture from ' + fileurl

def setWallpaperOfToday():
filename = STOREDIR + str(calendar.datetime.date.today()) + '.jpg'
print filename
getPicture(filename)
setWallPaper(filename)

setWallpaperOfToday()
print 'Wallpaper set ok!'

posted @ 2008-01-14 23:12  Yoshow  阅读(650)  评论(0编辑  收藏  举报