Created
October 12, 2010 02:27
-
-
Save mdellavo/621565 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
from itertools import chain | |
from random import shuffle | |
HEAD_XML = ''' | |
<background> | |
<starttime> | |
<year>2009</year> | |
<month>08</month> | |
<day>04</day> | |
<hour>00</hour> | |
<minute>00</minute> | |
<second>00</second> | |
</starttime> | |
''' | |
TAIL_XML = ''' | |
</background> | |
''' | |
NODE_XML = ''' | |
<static> | |
<duration>%(duration)s</duration> | |
<file>%(a)s</file> | |
</static> | |
<transition> | |
<duration>%(transition_duration)s</duration> | |
<from>%(a)s</from> | |
<to>%(b)s</to> | |
</transition> | |
''' | |
def is_image(s): | |
return s.endswith('.jpg') or s.endswith('.png') | |
def find_images(root): | |
for path, subdirs, filenames in os.walk(root): | |
for filename in filenames: | |
file_path = os.path.join(path, filename) | |
if is_image(file_path): | |
yield file_path | |
def emit(a, b, duration, transition_duration): | |
return NODE_XML % { 'duration' : duration, | |
'transition_duration' : transition_duration, | |
'a' : a , | |
'b' : b } | |
def pairs(xs): | |
''' | |
Break a sequence into pairs | |
''' | |
return zip(xs, xs[1:]) | |
def main(options, args): | |
images = list(chain(*(find_images(i) for i in args))) | |
shuffle(images) | |
duration = (24 * 60 * 60) / float(len(images)) | |
transition_duration = 5.0 | |
nodes = ( emit(a, b, duration, transition_duration) | |
for a, b in pairs(images) ) | |
print HEAD_XML | |
print "\n".join(nodes) | |
print TAIL_XML | |
return 0 | |
if __name__ == '__main__': | |
rv = main(None, sys.argv[1:]) | |
sys.exit(rv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment