每天换一个新桌面的python

本文介绍了一个使用Python编写的脚本,该脚本可以自动从国家地理杂志的每日图片中下载图片,并将其设置为桌面壁纸。此脚本首先安装必要的Python库,然后通过解析网页源代码获取图片链接并下载图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 代码
 
  1. """ 
  2. Set the wallpaper as the picture got from national geographic picture of the day 
  3. by seasons@gmail.com 
  4. """  
  5. import ctypes  
  6. import Image  
  7. import calendar  
  8. import urllib  
  9. import socket  
  10.   
  11. STOREDIR = 'C:/lg_wallpaper/pod/'  
  12.   
  13. def setWallpaperFromBMP(imagepath):  
  14.     SPI_SETDESKWALLPAPER = 20 # According to http://support.microsoft.com/default.aspx?scid=97142  
  15.     ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagepath , 0) #SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE  
  16.   
  17. def setWallPaper(imagePath):  
  18.     """Given a path to an image, convert it to bmp and set it as wallpaper"""  
  19.     bmpImage = Image.open(imagePath)  
  20.     newPath = STOREDIR + 'mywallpaper.bmp'  
  21.     bmpImage.save(newPath, "BMP")  
  22.     setWallpaperFromBMP(newPath)  
  23.       
  24. def getPicture(fname):  
  25.     sock = urllib.urlopen("http://lava.nationalgeographic.com/pod/index.html")  
  26.     htmlSource = sock.read()  
  27.     sock.close()      
  28.     pos1 = htmlSource.find('sm_wallpaper')  
  29.     pos2 = htmlSource.find('/pod/pictures/normal')  
  30.     filename = htmlSource[pos1+13:pos2-12]  
  31.     fileurl = 'http://lava.nationalgeographic.com/pod/pictures/lg_wallpaper/'+filename  
  32.     urllib.urlretrieve(fileurl, fname)  
  33.     print 'got the picture from ' + fileurl  
  34.   
  35. def setWallpaperOfToday():  
  36.     filename = STOREDIR + str(calendar.datetime.date.today()) + '.jpg'  
  37.     print filename  
  38.     getPicture(filename)  
  39.     setWallPaper(filename)  
  40.      
  41. setWallpaperOfToday()  
  42. print 'Wallpaper set ok!'  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值