[pygtk] Scaling SVG with rsvg and wxPython

Richard Lincoln r.w.lincoln at gmail.com
Tue Apr 24 17:36:10 WST 2007


I have written this script for rendering a SVG in a wxPython frame:

#!/usr/bin/env python

import wx
import rsvg
import cairo
import numpy

class TestFrame(wx.Frame):
    def __init__(self, parent, id, title, pos, size):
        wx.Frame.__init__(self, parent, id, title, pos, size)

        imgW = size[0]
        imgH = size[1]
        brga = numpy.ndarray(shape=(imgH,imgW,4), dtype=numpy.uint8)
        brga[:,:,0].fill(180) #blue
        brga[:,:,1].fill(105) #green
        brga[:,:,2].fill(255) #red
        rgba = numpy.ndarray(shape=(imgH,imgW,4), dtype=numpy.uint8)
        data24 = numpy.ndarray(shape=(imgH,imgW,3), dtype=numpy.uint8)
                stride = imgW * 4

        surface = cairo.ImageSurface.create_for_data(brga,
                                                     cairo.FORMAT_ARGB32,
                                                     imgW,
                                                     imgH,
                                                     stride)
        ctx = cairo.Context(surface)
        rsvg.set_default_dpi(900)
        svg = rsvg.Handle('tiger.svg')
        svg.render_cairo(ctx)

        rgba[:,:,0] = brga[:,:,2]
        rgba[:,:,1] = brga[:,:,1]
        rgba[:,:,2] = brga[:,:,0]
        rgba[:,:,3] = brga[:,:,3] #alpha

        data24[:,:,0] = brga[:,:,2]
        data24[:,:,1] = brga[:,:,1]
        data24[:,:,2] = brga[:,:,0]

        img = wx.ImageFromData(imgW, imgH, data24)
        bmp = wx.BitmapFromImage(img)
#        bmp = wx.BitmapFromBufferRGBA(imgW, imgH, rgba) #wxPython 2.8

        static_bmp = wx.StaticBitmap(self, -1, bmp)
        self.Show(True)

if __name__ == '__main__':
        app = wx.PySimpleApp()
        frame = TestFrame(None, wx.ID_ANY, 'test rsvg', (200, 200), (800, 800))
        app.MainLoop()


I see in the librsvg API doc that there is a function RsvgSizeFunc()
which I imagine can be used for changing the image size.

Can anyone tell me how I might go about scaling my rendered image
using the python bindings as I would like to be able to zoom in and
out?

Regards,

Richard


More information about the pygtk mailing list