import svgwrite
import Image, numpy

idx = 0
path = '.'
img_fn = path+'/'+('phoen%d.png' % idx)
mat_fn = path+'/'+('phoen%d-orient.txt' % idx)
out_fn = 'phoen%d.svg' % idx

img = Image.open(img_fn)
(sy,sx) = numpy.asarray(img).shape

mat_f = open(mat_fn,'r')
mat = []
for line in mat_f:
  mat += line.split()
mat_f.close()

# create all-purpose SVG object
svg = svgwrite.Drawing(out_fn)
# specify style once, for lines in world space
wspc = svgwrite.container.Group(style="stroke:rgb(255,0,0); stroke-width:0.02; stroke-linecap:round")
# corresponds to sampling rate; Y scaling is negative to account
# for how in SVG the Y coordinate increases *downward*, not upward
wspc.scale(100, -100)
# hey, this should ideally respect what orientation does with image corners
wspc.translate(3, -2.5)
svg.add(wspc)

# specify style once, for lines in index space
ispc = svgwrite.container.Group(style="stroke:rgb(0,255,0); stroke-width:3; stroke-linecap:round")
ispc.matrix(mat[0], mat[3], mat[1], mat[4], mat[2], mat[5])
wspc.add(ispc)

# add image to index space (where it belongs),
# and lines in index space (NOT a good idea)
img = svg.image(img_fn, insert=(-0.5,-0.5), size=(sx, sy))
ispc.add(img)
ispc.add(svg.line(start=(80,80), end=(90,80)))
ispc.add(svg.line(start=(90,80), end=(90,95)))
# can over-ride style in group for specific lines
ispc.add(svg.line(start=(90,95), end=(75,95), style="stroke:rgb(0,0,255); stroke-width:4;"))
ispc.add(svg.line(start=(75,95), end=(75,75)))

# add line in world space (where they belong)
wspc.add(svg.line(start=(0.025,2.0), end=(2.125,-1.654)))

svg.save()
