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
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!'