用Python設定案頭牆紙

來源:互聯網
上載者:User

網上看的一段比較好玩的代碼, 利用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!'

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.