image processing - How can i read svg data stroke in pycairo? -
image processing - How can i read svg data stroke in pycairo? -
i have jpg images , inputsvgdraw, flash tool image annotation (http://www.mainada.net/inputdraw), can trace lines on generate svg datas.
svg datas sample:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 488 325"><g fill="none" stroke-miterlimit="6" stroke-linecap="round" stroke-linejoin="round"><path d="m 307 97 l 0 -1 l -2 -1 l -10 -2 l -20 -1 l -25 5 l -22 9 l -10 9 l 0 9 l 2 12 l 16 18 l 25 11 l 25 5 l 17 -1 l 6 -4 l 3 -7 l -1 -12 l -6 -16 l -7 -13 l -11 -12 l -11 -14 l -9 -5" opacity="1" stroke="rgb(170,37,34)" stroke-width="5"/></g></svg>.
what function can manage info ?
you can read svg input using librsvg
, rendering cairo
. if want draw annotations in svg on initial image, might need utilize pil
numpy
since cairo
doesn't load many different image formats.
following illustration achieves (the difference tested ad-hoc ctypes
wrapper rsvg
):
import sys import rsvg import cairo import numpy pil import image # load image supposedly has same width , height svg one. img_rgba = numpy.array(image.open(sys.argv[1]).convert('rgba')) info = numpy.array(img_rgba.tostring('raw', 'bgra')) width, height = img_rgba.size surface = cairo.imagesurface.create_for_data(data, cairo.format_argb32, width, height) ctx = cairo.context(surface) # "paste" svg image. svg = rsvg.handle(file=sys.argv[2]) svg.render_cairo(ctx) surface.write_to_png(sys.argv[3])
image-processing svg pycairo
Comments
Post a Comment