ubuntu12.04 自定义动态壁纸

最近刚装了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"



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值