comparison regenerate-pngs.py @ 33:c8436f1752d7

Add special handling for different sized sprites.
author Simon Cross <hodgestar@gmail.com>
date Sun, 30 Aug 2009 19:05:31 +0000
parents 53960047c186
children 030bea282f28
comparison
equal deleted inserted replaced
32:f5f74f1f3a0b 33:c8436f1752d7
4 import rsvg 4 import rsvg
5 import os 5 import os
6 6
7 def svg_to_png(svg_name, png_name, w, h): 7 def svg_to_png(svg_name, png_name, w, h):
8 """Convert an SVG file to a PNG file.""" 8 """Convert an SVG file to a PNG file."""
9 print "Generating %s at %dx%d..." % (png_name, w, h)
9 r = rsvg.Handle(svg_name) 10 r = rsvg.Handle(svg_name)
10 11
11 scale = max(float(r.props.width) / w, float(r.props.height) / h) 12 scale = max(float(r.props.width) / w, float(r.props.height) / h)
12 scale = 1.0 / scale 13 scale = 1.0 / scale
13 14
18 ctx = cairo.Context(cs) 19 ctx = cairo.Context(cs)
19 ctx.scale(scale, scale) 20 ctx.scale(scale, scale)
20 r.render_cairo(ctx) 21 r.render_cairo(ctx)
21 cs.write_to_png(png_name) 22 cs.write_to_png(png_name)
22 23
23 def main(path, width, height): 24 def process_svg_folder(path, width, height):
24 for dirpath, dirnames, filenames in os.walk(path): 25 for dirpath, dirnames, filenames in os.walk(path):
25 for filename in filenames: 26 for filename in filenames:
26 basename, ext = os.path.splitext(filename) 27 basename, ext = os.path.splitext(filename)
27 if ext == ".svg": 28 if ext == ".svg":
28 svg_name = os.path.join(dirpath, basename + ".svg") 29 svg_name = os.path.join(dirpath, basename + ".svg")
29 png_name = os.path.join(dirpath, basename + ".png") 30 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) 31 svg_to_png(svg_name, png_name, width, height)
32 32
33 def process_sprite(name, width, height, sprite_path):
34 svg_name = os.path.join(sprite_path, name) + ".svg"
35 png_name = os.path.join(sprite_path, name) + ".png"
36 svg_to_png(svg_name, png_name, width, height)
37
33 if __name__ == "__main__": 38 if __name__ == "__main__":
34 main("data", 20, 20) 39 tile_path = "data/tiles"
40 sprite_path = "data/sprites"
41 sprites = [
42 ("chkn", 20, 20),
43 ("fox", 20, 20),
44 ("henhouse", 60, 40),
45 ]
46
47 process_svg_folder("data/tiles", 20, 20)
48 for name, width, height in sprites:
49 process_sprite(name, width, height, sprite_path)