Re: SVG PIL decoder

2009-09-30 Thread Donn
On Wednesday 30 September 2009 18:01:50 Patrick Sabin wrote:
 I would like to open svg files with PIL, but svg doesn't seem to be
 supported. Does anyone know about a svg decoder for the PIL?
Have a look at Cairo (python-cairo) in conjunction with librsvg (python-rsvg) 
-- that'll fix you up. You can go from an SVG to a PNG/array and thence into 
PIL if you need to.

\d
-- 
home: http://otherwise.relics.co.za/
2D vector animation : https://savannah.nongnu.org/projects/things/
Font manager : https://savannah.nongnu.org/projects/fontypython/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SVG PIL decoder

2009-09-30 Thread Patrick Sabin

Donn wrote:
Have a look at Cairo (python-cairo) in conjunction with librsvg (python-rsvg) 
-- that'll fix you up. You can go from an SVG to a PNG/array and thence into 
PIL if you need to.


Thanks for the tip. Got it work, although it was a bit tricky, as 
resizing doesn't seem to be supported by python-rsvg and 
cairo.ImageSurface.create_from_png doesn't allow StringIO or 
TemporaryFile for some reason (got Memory Error). So the code, if 
someone else needs it or someone can improve it:


def open_svg_as_image(fn, width, height):
tmpfd, tmppath = tempfile.mkstemp(.png)
tmpfile = os.fdopen(tmpfd,'w')

file = StringIO.StringIO()
svgsurface = cairo.SVGSurface (file, width, height)
svgctx = cairo.Context(svgsurface)
svg = rsvg.Handle(file=fn)
svgwidth = svg.get_property('width')
svgheight = svg.get_property('height')
svgctx.scale(width/float(svgwidth),height/float(svgheight))
svg.render_cairo(svgctx)

svgsurface.write_to_png(tmpfile)
tmpfile.close()
svgsurface.finish()

tmpfile = open(tmppath, 'r')
imgsurface = cairo.ImageSurface.create_from_png(tmpfile)
imgwidth = imgsurface.get_width()
imgheight = imgsurface.get_height()

data = imgsurface.get_data()

im = Image.frombuffer(RGBA,(imgwidth, imgheight),
data ,raw,RGBA,0,1)
os.remove(tmppath)
return im

--
http://mail.python.org/mailman/listinfo/python-list


Re: SVG PIL decoder

2009-09-30 Thread Donn
On Thursday 01 October 2009 01:08:28 Patrick Sabin wrote:
 Thanks for the tip. Got it work, although it was a bit tricky, as
 resizing doesn't seem to be supported by python-rsvg and
 cairo.ImageSurface.create_from_png doesn't allow StringIO or
My best suggestions are to visit the Cairo website -- inside there somewhere 
is a recipe page with many samples in Python. 

Next would be  http://www.tortall.net/mu/wiki/CairoTutorial. 

Third is a tutorial I made (perhaps less useful) on my site 
http://otherwise.relics.co.za/wiki/Tuts/Python/Cairo/ links at bottom of that 
page

Fourth is to join the ca...@cairographics.org mailing list at 
http://lists.cairographics.org/mailman/listinfo/cairo they are super helpful.

Lastly is my animation API (in sig)which is also Python and may help you with 
the source.

The general idea for scaling is to use matrices (cairo provides all commands) 
and then output the surface to a file-like object.

My animation API brings selected snippets of SVG in from an Inkscape file 
(tagged by id), animates them by tweening and can output each frame to another 
SVG or to a PNG.

HTH,
\d
-- 
home: http://otherwise.relics.co.za/
2D vector animation : https://savannah.nongnu.org/projects/things/
Font manager : https://savannah.nongnu.org/projects/fontypython/
-- 
http://mail.python.org/mailman/listinfo/python-list