def get_numpy_array(self, size=256, scale=0.333):
"""
using rsvg library, render the svg into a cairo surface with memory
defined by a numpy array, effectively rendering the svg to an array
"""
if self.tree_root is None:
self.tree_root = self.tree_org.getroot()
# get the result into a svg object (using rsvg lib)
svg_object = rsvg.Handle(ET.tostring(self.tree_root))
# create a numpy array to use as our canvas
data = np.zeros((size, size, 4), dtype=np.uint8)
surface = cairo.ImageSurface.create_for_data(
data, cairo.FORMAT_ARGB32, size, size)
cr = cairo.Context(surface)
# fill with solid white and set scale (magic scale number)
cr.set_source_rgb(1.0, 1.0, 1.0)
cr.paint()
cr.scale(scale, scale)
# render our manipulated svg into cairo surface
svg_object.render_cairo(cr)
return data
评论列表
文章目录