Variable Television

From XPUB & Lens-Based wiki
Revision as of 17:38, 20 October 2011 by Michael Murtaugh (talk | contribs)

Example of using Python & lxml.etree to manipulate an SVG file, rendering first as PNG frames via Inkscape, and as a video with ffmpeg.

SVG file (uses the font Crickx)

import lxml.etree
import random, os

doc = lxml.etree.parse("vtv.svg")
text = doc.find("//*[@id='textblock']")
background = doc.find("//*[@id='background']")

c = 0
for i in range(100):
    if i % 2:
        text.set("transform", "rotate(0)")
    else:
        text.set("transform", "rotate(180 512 670)")
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    style = "fill:#{:02X}{:02X}{:02X};fill-opacity:1;stroke:none".format(r, g, b)
    background.set("style", style)

    svg = lxml.etree.tostring(doc, encoding="utf-8")
    c += 1
    filename = "vtv{:02d}.svg".format(c)
    of = open(filename, "w")
    of.write(svg)
    of.close()

    # Use Inkscape to convert SVG to PNG
    os.system("inkscape -e vtv{0:02d}.png vtv{0:02d}.svg".format(c))

Command to convert the images to a video:

ffmpeg -r 10 -b 1800 -i vtv%02d.png VariableTelevision.ogg