原来发过一个Python下的换桌面脚本,由于相关网站改版,脚本失效.。我也相应更新一下。移除了图片质量一般的EPOD。充分测试了windows(XP) 和 linux(Ubuntu gnome)双平台. 同时将原有的拉伸全屏模式,更改为保持长宽比的居中模式(其他模式的使用参数也已写在注释中)。 对于APOD,可以手动指定更新任意一天的图片(只支持JPG), 而NGPOD的网站地址与时间无关,只能获取最新图片。下载前会检查文件名,如果图片已经存在,则自动跳过。默认下载大图,然后根据设定分辨率生成桌面图片。各项参数要在源码中自行调整。仍需要PIL模块支持,这个模块的详细介绍可以看这里。新脚本点此下载。
以Windows为例,正常输出有下面三种:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 获取指定日期的APOD D:\mine\Macro>python wpod.py Astronomy POD 081129 Retrieving the picture from http://apod.nasa.gov/apod/image/0811/VenJpt_beletsky.jpg Local file is D:\mine\Macro\VenJpt_beletsky.jpg Wallpaper is set! # 由于时差,东半球用户更新时,APOD服务器可能还没有准备好 D:\mine\Macro>python wpod.py Astronomy POD 100818 Sever haven't updated yet Nothing done. # 选择NGPOD, 不是每天的照片都有壁纸下载的。 D:\mine\Macro>python wpod.py National Geographic POD 100818 Not so attractive ... The file is already there. Local file is D:\mine\Macro\st-maarten-jet-fly-over_23943_990x742.jpg Wallpaper is set! |
.
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# -*- coding: utf-8 -*- # Set the wallpaper for Windows/Linux(gnome) # with APOD or NGPOD # by henrysting 2010-08-05 import ctypes import urllib import socket import os import time if os.name=="nt": import Image import _winreg def ftime(): str = time.strftime('%y%m%d', time.localtime()) return str def convertToBMP(imagePath): im = Image.open(imagePath) w,h = im.size if w > screen[0]: #width conformity whRatio = float(w)/h im = im.resize((screen[0],int(float(screen[0])/whRatio)),Image.BILINEAR) if im.size[1] > screen[1]: #height conformity im = im.resize(int(screen[1]*whRatio),screen[1]) newPath = STOREDIR + '\wallpaper.bmp' im.save(newPath,'BMP') return newPath def registerCentered(): # changes the registry to center wallpaper. # Stretch is 2 0,Tiled is 0 1. wallpaperStyle = '1' tileWallpaper = '0' desktopKey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Control Panel\\Desktop',0,_winreg.KEY_SET_VALUE) _winreg.SetValueEx(desktopKey,'WallpaperStyle',0,_winreg.REG_SZ,wallpaperStyle) _winreg.SetValueEx(desktopKey,'TileWallpaper',0,_winreg.REG_SZ,tileWallpaper) return 1 def getPicture(): if TYPE == 0: homepage = 'http://apod.nasa.gov/apod/' siteurl = 'http://apod.nasa.gov/apod/ap'+date+'.html' sock = urllib.urlopen(siteurl) htmlSource = sock.read() if htmlSource.find('404 Not Found')+1: print "Sever haven't updated yet" return '' sock.close() pos1 = htmlSource.find('href="image/') pos2 = htmlSource.find('.jpg"',pos1) page2 = htmlSource[pos1+6:pos2+4] filename = htmlSource[pos1+17:pos2+4] fileurl = homepage+page2 elif TYPE == 1: siteurl = 'http://photography.nationalgeographic.com' sock = urllib.urlopen("http://photography.nationalgeographic.com/photography/photo-of-the-day") htmlSource = sock.read() sock.close() pos0 = htmlSource.find('download_link') if pos0+1: print 'There is a Wallpaper today!' pos1 = htmlSource.find('href="http://images.',pos0) pos2 = htmlSource.find('.jpg">Download',pos0) else: print 'Not so attractive ...' pos0 = htmlSource.find('primary_photo') pos1 = htmlSource.find(' src="http://images.',pos0) pos2 = htmlSource.find('.jpg"',pos0) fileurl = htmlSource[pos1+6:pos2+4] filename = htmlSource[pos1+79:pos2+4] else: print 'Wrong Type Number!' try: if os.name=="nt": fname = STOREDIR + '\\'+ filename elif os.name=="posix": fname = STOREDIR + '/'+ filename if not os.path.exists(fname): print 'Retrieving the picture from ' + fileurl urllib.urlretrieve(fileurl, fname) else: print 'The file is already there.' return fname except: print filename print fileurl print fname print "Failed to retrieve the picture of ",date pass return fname def setWallpaperFromBMP(imagepath): SPI_SETDESKWALLPAPER = 20 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagepath , 0) def setWallPaper(imagePath): if os.name=="nt": registerCentered() newPath = convertToBMP(imagePath) setWallpaperFromBMP(newPath) elif os.name=="posix": try: os.system("gconftool-2 -t string -s /desktop/gnome/background/picture_filename \""+imagePath+"\" -s /desktop/gnome/background/picture_options scaled") # option can also be centered,stretched,tiled etc. except: os.system("dcop kdesktop KBackgroundIface setWallpaper \""+imagePath+"\" 7") def setWallpaperOfToday(): fname = getPicture() if fname != '': print 'Local file is ' + fname setWallPaper(fname) print 'Wallpaper is set!' else: print 'Nothing done.' if __name__ == "__main__": STOREDIR = os.getcwd() sites = ['Astronomy POD','National Geographic POD'] TYPE = 1 screen = (1280,1024) # 屏幕分辨率 date = '' # format should be 'yymmdd' if date == '': date = ftime() print sites[TYPE],date setWallpaperOfToday() |