Mercurial > rinkhals
view regenerate-pngs.py @ 44:7e884084e7b1
Move animal sprites to imagecache.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 16:49:59 +0000 |
parents | c8436f1752d7 |
children | 030bea282f28 |
line wrap: on
line source
#!/usr/bin/env python import cairo import rsvg import os def svg_to_png(svg_name, png_name, w, h): """Convert an SVG file to a PNG file.""" print "Generating %s at %dx%d..." % (png_name, w, h) r = rsvg.Handle(svg_name) scale = max(float(r.props.width) / w, float(r.props.height) / h) scale = 1.0 / scale r.props.dpi_x = r.props.dpi_x / scale r.props.dpi_y = r.props.dpi_y / scale cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(cs) ctx.scale(scale, scale) r.render_cairo(ctx) cs.write_to_png(png_name) def process_svg_folder(path, width, height): for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: basename, ext = os.path.splitext(filename) if ext == ".svg": svg_name = os.path.join(dirpath, basename + ".svg") png_name = os.path.join(dirpath, basename + ".png") svg_to_png(svg_name, png_name, width, height) def process_sprite(name, width, height, sprite_path): svg_name = os.path.join(sprite_path, name) + ".svg" png_name = os.path.join(sprite_path, name) + ".png" svg_to_png(svg_name, png_name, width, height) if __name__ == "__main__": tile_path = "data/tiles" sprite_path = "data/sprites" sprites = [ ("chkn", 20, 20), ("fox", 20, 20), ("henhouse", 60, 40), ] process_svg_folder("data/tiles", 20, 20) for name, width, height in sprites: process_sprite(name, width, height, sprite_path)