Mercurial > rinkhals
comparison regenerate_pngs.py @ 261:86291107d67a
Make regenerate_pngs more import-friendly.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 05 Sep 2009 13:20:59 +0000 |
parents | regenerate-pngs.py@d564ae258471 |
children | 8896bae31eda |
comparison
equal
deleted
inserted
replaced
260:fb33aef5131c | 261:86291107d67a |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import cairo | |
4 import rsvg | |
5 import os | |
6 from Image import open | |
7 | |
8 def svg_to_png(svg_name, png_name, w, h): | |
9 """Convert an SVG file to a PNG file.""" | |
10 print "Generating %s at %dx%d..." % (png_name, w, h) | |
11 r = rsvg.Handle(svg_name) | |
12 | |
13 scale = max(float(r.props.width) / w, float(r.props.height) / h) | |
14 scale = 1.0 / scale | |
15 | |
16 r.props.dpi_x = r.props.dpi_x / scale | |
17 r.props.dpi_y = r.props.dpi_y / scale | |
18 | |
19 cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) | |
20 ctx = cairo.Context(cs) | |
21 ctx.scale(scale, scale) | |
22 r.render_cairo(ctx) | |
23 cs.write_to_png(png_name) | |
24 | |
25 def process_svg_folder(path, width, height): | |
26 for dirpath, dirnames, filenames in os.walk(path): | |
27 for filename in filenames: | |
28 basename, ext = os.path.splitext(filename) | |
29 if ext == ".svg": | |
30 svg_name = os.path.join(dirpath, basename + ".svg") | |
31 png_name = os.path.join(dirpath, basename + ".png") | |
32 svg_to_png(svg_name, png_name, width, height) | |
33 | |
34 def process_sprite(name, width, height, sprite_path): | |
35 svg_name = os.path.join(sprite_path, name) + ".svg" | |
36 png_name = os.path.join(sprite_path, name) + ".png" | |
37 svg_to_png(svg_name, png_name, width, height) | |
38 | |
39 def process_cursor(name, width, height, sprite_path, cursor_path): | |
40 # We bounce through png to get something PIL understands | |
41 svg_name = os.path.join(sprite_path, name) + '.svg' | |
42 png_name = os.path.join(cursor_path, name) + '.png' | |
43 xbm_name = os.path.join(cursor_path, name) + '.xbm' | |
44 svg_to_png(svg_name, png_name, width, height) | |
45 # We need to bounce through 'L' first to handle transparency OK | |
46 pixeldata = open(png_name).convert('L') | |
47 # Everything > 0 goes to white | |
48 lut = [0] + [1]*255 | |
49 pixeldata.point(lut, mode='1').save(xbm_name) | |
50 os.remove(png_name) | |
51 | |
52 TILE_PATH = "data/tiles" | |
53 SPRITE_PATH = "data/sprites" | |
54 IMAGE_PATH = "data/images" | |
55 CURSOR_PATH = "data/cursors" | |
56 | |
57 SPRITES = [ | |
58 # chicken bits | |
59 ("chkn", 20, 20), | |
60 ("wing", 20, 20), | |
61 ("eye", 20, 20), | |
62 ("equip_rifle", 20, 20), | |
63 ("equip_knife", 20, 20), | |
64 ("equip_kevlar", 20, 20), | |
65 ("equip_helmet", 20, 20), | |
66 ("select_chkn", 20, 20), | |
67 ("nest", 20, 20), | |
68 ("equip_egg", 20, 20), | |
69 # fox bits | |
70 ("fox", 20, 20), | |
71 ("ninja_fox", 20, 20), | |
72 ("sapper_fox", 20, 20), | |
73 ("rinkhals", 20, 20), | |
74 # buildings | |
75 ("henhouse", 60, 40), | |
76 ("select_henhouse", 60, 40), | |
77 ("hendominium", 40, 60), | |
78 ("select_hendominium", 40, 60), | |
79 ("watchtower", 40, 40), | |
80 ("select_watchtower", 40, 40), | |
81 # special effects | |
82 ("muzzle_flash", 20, 20), | |
83 ("chkn_death", 20, 20), | |
84 ("fox_death", 20, 20), | |
85 # other | |
86 ("egg", 20, 20), | |
87 ] | |
88 | |
89 CURSORS = [ | |
90 ("chkn", 16, 16), | |
91 ("egg", 16, 16), | |
92 ("sell_cursor", 24, 24), | |
93 ] | |
94 | |
95 if __name__ == "__main__": | |
96 process_svg_folder("data/tiles", 20, 20) | |
97 process_svg_folder("data/icons", 40, 40) | |
98 for name, width, height in SPRITES: | |
99 process_sprite(name, width, height, SPRITE_PATH) | |
100 process_sprite("splash", 800, 600, IMAGE_PATH) | |
101 process_sprite("gameover_win", 800, 600, IMAGE_PATH) | |
102 process_sprite("gameover_lose", 800, 600, IMAGE_PATH) | |
103 for name, width, height in CURSORS: | |
104 process_cursor(name, width, height, SPRITE_PATH, CURSOR_PATH) |