I am also trying to solve this problem, except for images. The question of recursion is that you need to dot multiply the transformation matricies. I'm using NumPy. However, my computations are coming up with ostensibly incorrect answers. Maybe you'll have more luck.
http://www.scipy.org/Tentative_NumPy_Tutorial
http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined
from decimal import Decimal
import xml.dom.minidom as dom
from numpy import *
doc = dom.parse("labels.svg")
def walk(node):
if node.nodeType != 1:
return
if node.tagName == 'image':
href = node.getAttribute('xlink:href')
if not href.startswith("labels/"):
return
name = (
href.
replace('labels/', '').
replace('.png', '').
replace('-resized', '').
replace('-normalized', '')
)
left = float(node.getAttribute('x'))
top = float(node.getAttribute('y'))
position = matrix([left, top, float(1)])
width = float(node.getAttribute('width'))
height = float(node.getAttribute('height'))
size = matrix([left, top, float(1)])
transform = node.getAttribute('transform')
if transform:
a, b, c, d, e, f = map(float, transform
.replace('matrix(', '')
.replace(')', '')
.split(',')
)
transform = matrix([
[a, c, e],
[b, d, f],
[0, 0, 1]
])
left, top, _ = (transform.I * position.T).A1
print name, (left, top)
child = node.firstChild
while child:
walk(child)
child = child.nextSibling
walk(doc.documentElement)