Server side SVG for PNG (or some other image format) in python

I am currently using rsvg to load svg (from a string, not from a file) and draw in cairo. Does anyone know a better way? I use PIL elsewhere in my application, but I do not know how to do this with PIL.

+11
python png svg
May 28 '10 at 20:44
source share
4 answers

Here I have:

import cairo import rsvg def convert(data, ofile, maxwidth=0, maxheight=0): svg = rsvg.Handle(data=data) x = width = svg.props.width y = height = svg.props.height print "actual dims are " + str((width, height)) print "converting to " + str((maxwidth, maxheight)) yscale = xscale = 1 if (maxheight != 0 and width > maxwidth) or (maxheight != 0 and height > maxheight): x = maxwidth y = float(maxwidth)/float(width) * height print "first resize: " + str((x, y)) if y > maxheight: y = maxheight x = float(maxheight)/float(height) * width print "second resize: " + str((x, y)) xscale = float(x)/svg.props.width yscale = float(y)/svg.props.height surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, x, y) context = cairo.Context(surface) context.scale(xscale, yscale) svg.render_cairo(context) surface.write_to_png(ofile) 
+12
May 28 '10 at 20:52
source share

What about imagemagic? - http://www.imagemagick.org/script/magick-vector-graphics.php It can read / write from / to stdin / stdout, so you can integrate it with your application even if you do not want to use files

+2
May 28 '10 at 20:53
source share

You can also use PhantomJS (see http://phantomjs.org/screen-capture.html )

From the shell:

 phantomjs rasterize.js http://ariya.github.com/svg/tiger.svg tiger.png 

Or from python using selenium:

 from selenium import webdriver driver = webdriver.PhantomJS() driver.set_window_size(1024, 768) driver.get('http://ariya.github.com/svg/tiger.svg') driver.save_screenshot('tiger.png') 
+2
Mar 08 '14 at 8:14
source share

I have inkscape installed, so I just process the process with the inkscape command with inkscape -f file.svg -e file.png

Using this code:

 import subprocess inkscape_dir=r"C:\Program Files (x86)\Inkscape" assert os.path.isdir(inkscape_dir) os.chdir(inkscape_dir) subprocess.Popen(['inkscape.exe',"-f",fname,"-e",fname_png]) 

I am on Windows 7 and received a Windows 5 [Access Denied] error (or something like that) until I go to the inkscape directory

+1
Aug 20 2018-11-11T00:
source share



All Articles