Skip to content

Instantly share code, notes, and snippets.

@jkent
Last active December 10, 2015 11:09
Show Gist options
  • Save jkent/4425816 to your computer and use it in GitHub Desktop.
Save jkent/4425816 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ts=4 et sw=4 sts=4 ai
#
# Makes slideshow XML files for gnome/mate desktop using a
# parameter list of image files.
#
# Example usage;
# make_slideshow.py earthporn/* earthporn/slideshow.xml
#
IMAGE_TIME = 300
TRANSITION_TIME = 15
STARTTIME = (2009, 8, 4)
IMAGE_EXTENSIONS = (".bmp", ".gif", ".jpg", ".jpeg", ".png")
import sys
import os
from datetime import datetime
import xml.etree.ElementTree as ET
def starttime(parent, t):
if isinstance(t, tuple):
t = apply(datetime, t)
elm = ET.SubElement(parent, "starttime")
ET.SubElement(elm, "year").text = "%04d" % t.year
ET.SubElement(elm, "month").text = "%02d" % t.month
ET.SubElement(elm, "day").text = "%02d" % t.day
ET.SubElement(elm, "hour").text = "%02d" % t.hour
ET.SubElement(elm, "minute").text = "%02d" % t.minute
ET.SubElement(elm, "second").text = "%02d" % t.second
def static(parent, filename):
elm = ET.SubElement(parent, "static")
ET.SubElement(elm, "duration").text = "%.1f" % (IMAGE_TIME - TRANSITION_TIME)
ET.SubElement(elm, "file").text = os.path.abspath(filename)
def transition(parent, begin_filename, end_filename):
elm = ET.SubElement(parent, "transition")
ET.SubElement(elm, "duration").text = "%.1f" % TRANSITION_TIME
ET.SubElement(elm, "from").text = os.path.abspath(begin_filename)
ET.SubElement(elm, "to").text = os.path.abspath(end_filename)
if __name__ == '__main__':
if not sys.argv[1:]:
sys.exit("No files specified")
bg_files = sys.argv[1:]
if bg_files[-1].endswith('.xml'):
xml_file = bg_files[-1]
bg_files = bg_files[:-1]
else:
while True:
xml_file = raw_input("Output file [slideshow.xml]: ").strip()
if not xml_file:
xml_file = "slideshow.xml"
if xml_file.endswith(".xml"):
break
if not bg_files:
sys.exit("No background images provided")
bg_files.sort()
root = ET.Element("background")
tree = ET.ElementTree(root)
starttime(root, STARTTIME)
is_first = True
last_bg_file = None
for bg_file in bg_files[:]:
if not bg_file.lower()[-4:] in IMAGE_EXTENSIONS:
bg_files.remove(bg_file)
continue
if not is_first:
transition(root, last_bg_file, bg_file)
static(root, bg_file)
is_first = False
last_bg_file = bg_file
transition(root, last_bg_file, bg_files[0])
tree.write(xml_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment