我也在尝试解决这个问题,除了图像。递归的问题是您需要对转换矩阵进行点乘。我正在使用 NumPy。然而,我的计算得出了表面上不正确的答案。也许你会有更多的运气。
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)