comparison regenerate-pngs.py @ 18:53960047c186

Script for generating .png images from .svg.
author Simon Cross <hodgestar@gmail.com>
date Sun, 30 Aug 2009 17:26:05 +0000
parents
children c8436f1752d7
comparison
equal deleted inserted replaced
17:cbbc5da7708a 18:53960047c186
1 #!/usr/bin/env python
2
3 import cairo
4 import rsvg
5 import os
6
7 def svg_to_png(svg_name, png_name, w, h):
8 """Convert an SVG file to a PNG file."""
9 r = rsvg.Handle(svg_name)
10
11 scale = max(float(r.props.width) / w, float(r.props.height) / h)
12 scale = 1.0 / scale
13
14 r.props.dpi_x = r.props.dpi_x / scale
15 r.props.dpi_y = r.props.dpi_y / scale
16
17 cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
18 ctx = cairo.Context(cs)
19 ctx.scale(scale, scale)
20 r.render_cairo(ctx)
21 cs.write_to_png(png_name)
22
23 def main(path, width, height):
24 for dirpath, dirnames, filenames in os.walk(path):
25 for filename in filenames:
26 basename, ext = os.path.splitext(filename)
27 if ext == ".svg":
28 svg_name = os.path.join(dirpath, basename + ".svg")
29 png_name = os.path.join(dirpath, basename + ".png")
30 print "Generating %s at %dx%d..." % (png_name, width, height)
31 svg_to_png(svg_name, png_name, width, height)
32
33 if __name__ == "__main__":
34 main("data", 20, 20)