8 Jul 2007 frob   » (Journeyer)

pycairo snippet

On the way to Visio VSD/VSS viewer I need some way to load images in different formats into cairo context.
cworth pointed me to the right way to achieve it in C.
pyvsdump is in pythonese, so I made a small test and converted it to this 'snippet'.
It loads image file into memory (to emulate loading from parsed VSD) and after that display it.
Enjoy =)

[take a file]

-------------------------------------


#!/usr/bin/env python
import sys
import gtk
import cairo


def expose (da, event,pixbuf): ctx = da.window.cairo_create() ct = gtk.gdk.CairoContext(ctx) ct.set_source_pixbuf(pixbuf,0,0) ctx.paint() ctx.stroke()

def main(): filename = sys.argv[1] input = open(filename) imagebuf = input.read() pixbufloader = gtk.gdk.PixbufLoader() pixbufloader.write(imagebuf) pixbufloader.close() pixbuf = pixbufloader.get_pixbuf() imgw=pixbuf.get_width() imgh=pixbuf.get_height() win = gtk.Window() win.connect('destroy', gtk.main_quit) win.set_default_size(imgw, imgh) da = gtk.DrawingArea() win.add(da) da.connect('expose_event', expose, pixbuf) win.show_all() gtk.main()

if __name__ == '__main__': if len(sys.argv) != 2: program = sys.argv[0] print program +':', 'usage:', program, '<filename>' sys.exit(0) else: main()

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!