SVG Image Handler?

I'm developing an application with wxPython, which will make extensive use
of images. At the moment I am working mainly with .png files, but we hope
(eventually) to make use of .svg format. Might there be something like an
"svg image handler" within the current distribution of wxPython? If not, how
would you suggest handling the display of svg image image files with
wxPython? Thanks for any advice.

Tim Grove

Use aggdraw:

here is a little bit of code to get you started… :slight_smile: I use it to display the famous tiger.svg in an attempted project.

class Frame(object):
def init(self, parent=None, title="", shown=True, size=(800, 600)):

    self._back = wx.Frame(parent=parent, title=title)
   
    self._back.Bind(wx.EVT_SIZE, self.OnSize)
    self._back.Bind(wx.EVT_ERASE_BACKGROUND, self.DoNothing)
    self._back.Bind(

wx.EVT_PAINT, self.OnPaint)
self._back.SetSize(size)
self._back.Show(shown)
def DoNothing(self, evt): return None
def OnPaint(self, evt):
dc = wx.PaintDC(self._back)
offDC = wx.MemoryDC()
offDC.SelectObject(self.bmp)
w, h = self.size
dc.Blit(0, 0, w, h, offDC, 0, 0)

def OnSize(self, evt):
    self.size = evt.GetSize()
    self.Redraw

()

def Redraw(self):
    draw = aggdraw.Draw("RGB", self.size)
    svglines = file("tiger.svg").read().splitlines()[7:]
    try:
        i = 0
        fill = "#ffffff"

        stroke = "#ffffff"
        width = 1.
        b = aggdraw.Brush(fill)
        p = aggdraw.Pen(stroke, width)

        for line in svglines:
            if i%3 == 0:

                if "fill" in line:
                    fill = line[line.index("fill")+6:line.index("fill")+13]
                if "stroke" in line:
                    stroke = line[

line.index(“stroke”)+7:line.index(“stroke”)+14]
if “width” in line:
width = float(line[line.index(“width”)+6:-2])
b = aggdraw.Brush(fill)
p = aggdraw.Pen(stroke, width)
elif i%3 ==1:
s = aggdraw.Symbol(line[11:-3])
draw.symbol((200,200), s, p, b)
i +=1

    except:
        import traceback
        traceback.print_exc()
   
    img = wx.EmptyImage(self.size[0], self.size[1])
    img.SetData(draw.tostring())
    bmp = img.ConvertToBitmap

()
self.bmp = bmp

Timothy W. Grove wrote:

how
would you suggest handling the display of svg image image files with
wxPython? Thanks for any advice.

svg isn't an image format. It's just what it says: an XML format for describing Scalable Vector Graphics.

So you need two things:

(1) A way to parse the XML and turn it into some kind of data structure that describes the graphics,

(2) a way to render those graphics.

Peter mentioned aggdraw. aggdraw is a PIL wrapper around the antigrain geometry rendering library. Agg itself includes some SVG parsing capability, but I don't know if aggdraw includes that, by Peter's example, it doesn't appear to, so you still need to parse the XML somehow

aggdraw will give you very nice rendering, but little else.

Another option would be to use wx.lib.floatcanvas. The rendering won't be quite as nice (though I'm thinking of adding Agg to it some day), but you'll get the image in a zoomable scrollable window.

Either way you need to parse and interpret the SVG. If you want to support SVG in general, that's a major task! I'd look to see if you can find an external SVG rendering project that you can either wrap or just call externally to generate you images from SVG.

maybe librsvg ?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks Chris and Peter, I'll have a look at your suggestions. I need also to
take a closer look at the SVG "format" and see what I'm actually dealing
with; I don't think I'm going to find a package that will handle svg files
just like any other old "image".

I was interested to discover a python binding for librsvg (pyrsvg, of
course!), which I believe has only recently been released;
http://www.rittau.org/. Now I just have to figure out how to use it with
Windows. I haven't worked with Linux for awhile, and I've gotten too used to
packages with Windows installers!!! Time to get my hands dirty...

Best regards,
Tim

···

-----Original Message-----
From: Christopher Barker [mailto:Chris.Barker@noaa.gov]
Sent: 20 April 2006 19:02
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] SVG Image Handler?

Timothy W. Grove wrote:
> how
> would you suggest handling the display of svg image image files with
> wxPython? Thanks for any advice.

svg isn't an image format. It's just what it says: an XML format for
describing Scalable Vector Graphics.

So you need two things:

(1) A way to parse the XML and turn it into some kind of data structure
that describes the graphics,

(2) a way to render those graphics.

Peter mentioned aggdraw. aggdraw is a PIL wrapper around the antigrain
geometry rendering library. Agg itself includes some SVG parsing
capability, but I don't know if aggdraw includes that, by Peter's
example, it doesn't appear to, so you still need to parse the XML somehow

aggdraw will give you very nice rendering, but little else.

Another option would be to use wx.lib.floatcanvas. The rendering won't
be quite as nice (though I'm thinking of adding Agg to it some day), but
you'll get the image in a zoomable scrollable window.

Either way you need to parse and interpret the SVG. If you want to
support SVG in general, that's a major task! I'd look to see if you can
find an external SVG rendering project that you can either wrap or just
call externally to generate you images from SVG.

maybe librsvg ?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Hi Chris,

Thanks for your suggestions. I've started to play around with
wx.lib.floatcanvas with a view to displaying SVG, but can I also draw
Bitmaps to it? I see that NavCanvas inherits from wx.Panel, so I've tried
making use of wx.ClientDC, but I haven't gotten too far yet. Not the main
reason for using floatcanvas, I know, but I might need to do it
occasionally.

Best regards,
Tim

···

-----Original Message-----
From: Christopher Barker [mailto:Chris.Barker@noaa.gov]
Sent: 20 April 2006 19:02
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] SVG Image Handler?

Timothy W. Grove wrote:
> how
> would you suggest handling the display of svg image image files with
> wxPython? Thanks for any advice.

svg isn't an image format. It's just what it says: an XML format for
describing Scalable Vector Graphics.

So you need two things:

(1) A way to parse the XML and turn it into some kind of data structure
that describes the graphics,

(2) a way to render those graphics.

Peter mentioned aggdraw. aggdraw is a PIL wrapper around the antigrain
geometry rendering library. Agg itself includes some SVG parsing
capability, but I don't know if aggdraw includes that, by Peter's
example, it doesn't appear to, so you still need to parse the XML somehow

aggdraw will give you very nice rendering, but little else.

Another option would be to use wx.lib.floatcanvas. The rendering won't
be quite as nice (though I'm thinking of adding Agg to it some day), but
you'll get the image in a zoomable scrollable window.

Either way you need to parse and interpret the SVG. If you want to
support SVG in general, that's a major task! I'd look to see if you can
find an external SVG rendering project that you can either wrap or just
call externally to generate you images from SVG.

maybe librsvg ?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Timothy W. Grove wrote:

I've started to play around with
wx.lib.floatcanvas with a view to displaying SVG, but can I also draw
Bitmaps to it? I see that NavCanvas inherits from wx.Panel, so I've tried
making use of wx.ClientDC, but I haven't gotten too far yet.

you probably don't want to do that. You can draw directly to the panel in FloatCanvas, but you really need to know what you're doing. The whole point of FloatCanvas is that is object-persistent, and you don't have to deal with DCs.

Try the FloatCanvas.Bitmap and FloatCanvas.ScaledBitmap objects:

Mybmp = Canvas.AddScaledBitmap(bmp, (x,y), height=h, position="tl")

bmp can be a wxBitmap or wxImage.

It's in the demo. Note that if you use a large bitmap and zoom in on it, you can get some problems because it scales the bitmap first, then draws it, so it creates a HUGE version in memory, only to display a small portion of it. I'm working on fixing that right now.

I have an updated version available, along with some more demos, let me know if you'd like me to send it to you. Also, I have a small mailing list for FloatCanvas announcements, if you'd like to be on it.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov