import math import xml.etree.ElementTree as ET def writeBml(anim, filename): root = ET.Element('blm') root.attrib['width'] = str(anim.dimension.width) root.attrib['height'] = str(anim.dimension.height) bits = int(math.ceil(math.log(anim.dimension.depth, 2))) root.attrib['bits'] = str(bits) root.attrib['channels'] = str(anim.dimension.channels) header = ET.Element('header') for (name, val) in anim.tags: elem = ET.Element(name) elem.text = str(val) header.append(elem) root.append(header) for animFrame in anim: frame = ET.Element('frame') frame.attrib['duration'] = str(animFrame.delay) for y in range(anim.dimension.height): rowData = '' for x in range(anim.dimension.width): p = animFrame.getPixel(x,y) if type(p) == tuple or type(p) == list: for v in p: if bits >= 5: rowData += '%02x' % (v) else: rowData += '%x' % (v) else: if bits >= 5: rowData += '%02x' % (p) else: rowData += '%x' % (p) row = ET.Element('row') row.text = rowData frame.append(row) root.append(frame) f = open(filename, 'w') ET.ElementTree(root).write(f) f.close()