当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 3 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 在ubuntu中cosmos动态壁纸的python实现脚本
帖子发表于 : 2012-06-28 12:46 

注册: 2007-04-29 16:26
帖子: 39
送出感谢: 0 次
接收感谢: 0 次
在ubuntu10.04下cosmos的动态壁纸效果和ms的theme差不多。
系统默认的cosmos在/usr/shared/background下的xml配置。
直接编辑的话,图片太多的话,记不住也容易写错。
为了复习python,于是写了一个脚本。
在当前文件夹下,预先存放若干图片,类型是jpg的。


然后执行以下脚本。(请保存为abc.py,然后在shell中python abc.py执行,如果顺利,应该会产生bk.xml)
[python]
#coding=gbk
import sys
import os
import string
import shutil
import xml.etree.ElementTree as xml

def insertxmlEl(elParent,elName,elValue):
el = xml.Element(elName)
el.text = elValue
elParent.append(el)

def insertstaticEl(elParent,filename):
elStatic = xml.Element('static')
insertxmlEl(elStatic,'duration','1795')
insertxmlEl(elStatic,'file',filename)
elParent.append(elStatic)

def inserttransEl(elParent,fromFile,toFile):
elTrans = xml.Element('transition')
insertxmlEl(elTrans,'duration','5')
insertxmlEl(elTrans,'from',fromFile)
insertxmlEl(elTrans,'to',toFile)
elParent.append(elTrans)

def cosmmaker(path,filename):
root = xml.Element('background')
elStarttime = xml.Element('starttime')
insertxmlEl(elStarttime,'year','2009')
insertxmlEl(elStarttime,'month','08')
insertxmlEl(elStarttime,'day','04')
insertxmlEl(elStarttime,'month','00')
insertxmlEl(elStarttime,'minute','00')
insertxmlEl(elStarttime,'second','00')
root.append(elStarttime)

firstFile = ''
prevfile = ''
#枚举文件
for fileitem in os.listdir(path):
if(os.path.isfile(fileitem) == False):
continue;
filebasename , fext = os.path.splitext(fileitem);
#检查是否是jpg文件
if(string.lower(fext) != ".jpg"):
continue;
#如果文件名包含空格,就替换给下划线,然后重命名
if(string.find(filebasename," ")):
newfilename = string.replace(filebasename," ","_") + ".jpg"
targetfile = os.path.join(path,newfilename)
if(os.path.exists(targetfile) == False):
os.rename(os.path.join(path,fileitem),targetfile)
else:
newfilename = filebasename + ".jpg"
targetfile = os.path.join(path,newfilename)

insertstaticEl(root,targetfile)
if(firstFile == ''):
firstFile = targetfile
if(prevfile == ''):
prevfile = targetfile
else:
inserttransEl(root,prevfile,targetfile)

if(firstFile !=''):
inserttransEl(root,targetfile,firstFile)



#Open a file
file = open(filename, 'w+')
#Create an ElementTree object from the root element
xml.ElementTree(root).write(file)
#Close the file like a good programmer
file.close()



cosmmaker(os.getcwd() ,"bk.xml")

[/python]


最后更换壁纸,加入刚才生成的bk.xml。
于是属于我们自己的cosmos壁纸就有了。


附件:
cosmmaker.py [2.51 KiB]
被下载 46 次


最后由 febwave 编辑于 2012-06-28 16:27,总共编辑了 2 次
页首
 用户资料  
 
2 楼 
 文章标题 : Re: 在ubuntu中cosmos动态壁纸的python实现脚本
帖子发表于 : 2012-06-28 12:49 

注册: 2010-07-19 21:41
帖子: 22323
系统: OS X
送出感谢: 8
接收感谢: 42
好歹用下
代码:
[python][/python]
吧,否则真没法看…


_________________
NO DO NO DIE
http://a/%%30%30


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 在ubuntu中cosmos动态壁纸的python实现脚本
帖子发表于 : 2012-06-28 16:25 

注册: 2007-04-29 16:26
帖子: 39
送出感谢: 0 次
接收感谢: 0 次
不好意思,我对这个编辑功能不了解,不过我已经附加了py文件
to jtshs256:已经应用你的方法了.


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 3 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:yq-ysy 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译