最近刚装了Ubuntu12.04,好怀念之前在windows下的桌面背景(都是和朋友一起出去玩的照片~),从http://forum.ubuntu.org.cn/viewtopic.php?t=391446&p=2873883得知修改 /usr/share/backgrounds/contest/precise.xml 文件即可,但照片比较多,不想手动修改,便试着用python来完成这个任务,下面是我的成果。
"""
This is the "DIY_dynamic_wallpapers.py" which is used to set your favourite pictures as wallpapers.
First step:
put your wallpapers in one folder(such as /usr/home/ubuntu/myWallPapers)
Second step:
exec this moudle and generate the "precise.xml"
for example: DIY_dynamic_wallpapers("/usr/home/ubuntu/myWallPapers","/usr/home/ubuntu/precise.xml")
Third step:
move the "precise.xml" to /usr/share/backgrounds/contest/precise.xml
$:sudo mv /usr/home/ubuntu/precise.xml /usr/share/backgrounds/contest/precise.xml
Fourth step:
open the dialog which is used for changing the wallpaper,then select the image with a clock mark
@environment:
ubuntu12.04
python2.7
@refURLS:
http://forum.ubuntu.org.cn/viewtopic.php?t=391446&p=2873883
http://www.linuxidc.com/Linux/2012-05/59776.htm
http://www.51testing.com/?uid-138711-action-viewspace-itemid-173347
@author:aslily
"""
def DIY_dynamic_wallpapers(rootDir,outputPath):
#@rootDir: the directory which stores the background images
#@outputPath: the path for the newly precise.xml
import os
import os.path
from xml.dom.minidom import Document
#store full path names of the background images into pictureName
pictureName = []
for parent,dirnames,filenames in os.walk(rootDir):
for filename in filenames:
pictureName.insert(0,os.path.join(parent,filename))
"""
#test for pictureName
print 'first=' + pictureName[0]
for picture in pictureName:
print 'pn=' + picture
print 'last=' + pictureName[-1]
"""
#set the default time for changing picture
sDuration = "45.0" #one stays 45 seconds and then change to next
tDuration = "5.0" #change one to next in 5 seconds
#create xml doc
doc = Document()
#background node
background = doc.createElement("background")
doc.appendChild(background)
#starttime node
starttime = doc.createElement("starttime")
background.appendChild(starttime)
#tags in starttime
tagsInStart = [["year","2009"],["month","08"],["day","04"],
["hour","00"],["minute","00"],["second","00"]]
for tag in tagsInStart:
node = doc.createElement(tag[0])
nodeValue = doc.createTextNode(tag[1])
node.appendChild(nodeValue)
starttime.appendChild(node)
#create static nodes and transition nodes
pictureNum = len(pictureName)
for index in range(len(pictureName)):
#create static node
snode = doc.createElement("static")
dnode = doc.createElement("duration")
dvalue = doc.createTextNode(sDuration)
fnode = doc.createElement("file")
fvalue = doc.createTextNode(pictureName[index])
background.appendChild(snode)
snode.appendChild(dnode)
snode.appendChild(fnode)
dnode.appendChild(dvalue)
fnode.appendChild(fvalue)
#create transition node
tnode = doc.createElement("transition")
tdnode = doc.createElement("duration")
tdvalue = doc.createTextNode(tDuration)
fromnode = doc.createElement("from")
tonode = doc.createElement("to")
#set fromValue and toValue
if (index < pictureNum - 1):
fv = pictureName[index]
tv = pictureName[index+1]
else:
fv = pictureName[-1]
tv = pictureName[0]
fromValue = doc.createTextNode(fv)
toValue = doc.createTextNode(tv)
background.appendChild(tnode)
tnode.appendChild(tdnode)
tnode.appendChild(fromnode)
tnode.appendChild(tonode)
tdnode.appendChild(tdvalue)
fromnode.appendChild(fromValue)
tonode.appendChild(toValue)
#end for
#test for xml
#print doc.toprettyxml(indent=" ")
#output xml file to outputPath
with open(outputPath,"w") as f:
f.write(doc.toprettyxml(indent=" "))
#print that's ok
print "that's ok"