-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnw2png.py
195 lines (153 loc) · 5.71 KB
/
nw2png.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (c) 2016, Aeva M. Palecek
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
from PIL import Image
from util import load_level
from parser_common import setup_paths, TILE_SIZE
def make_box(x, y, w=TILE_SIZE, h=TILE_SIZE):
return map(int, (x, y, x+w, y+h))
def tile_box(x, y):
return make_box(x * TILE_SIZE, y * TILE_SIZE)
def tile_segments(tiles_path):
img = Image.open(tiles_path)
width = img.size[0] / TILE_SIZE
height = img.size[1] / TILE_SIZE
tiles = []
for x in range(width):
tiles.append([])
for y in range(height):
col = tiles[-1]
box = tile_box(x, y)
col.append(img.crop(box))
return tiles
def generate_map(board, tiles):
img = Image.new('RGBA',(64*16, 64*16))
for x in range(64):
for y in range(64):
tile_x, tile_y = board[x][y]
try:
tile = tiles[tile_x][tile_y]
box = tile_box(x, y)
img.paste(tile, box)
except:
pass
return img
def apply_effect(img, effect):
px = img.load()
for x in range(img.size[0]):
for y in range(img.size[1]):
old = map(lambda x: x/255.0, px[x,y])
new = [int(old[i] * effect[i]*255) for i in range(4)]
px[x,y] = tuple(new)
def clamp(value, high=1.0, low=0.0):
return max(min(value, high), low)
def apply_area_effect(img, effect):
data = img.load()
for x in range(img.size[0]):
for y in range(img.size[1]):
pixel = []
alpha = clamp(1.0-effect[3])
for c in range(3):
darkened = data[x,y][c] * alpha
tinted = darkened + (effect[c] * 255)
pixel.append(min(int(tinted), 255))
data[x,y] = tuple(pixel)
def add_composite(dest, blit):
"""
Takes two PIL Image objects of the same size as arguments and adds
their color channels together, and returns a 3rd image.
"""
assert dest.size == blit.size
bg_data = dest.load()
fg_data = blit.load()
out = Image.new('RGBA', blit.size)
out_data = out.load()
for x in range(blit.size[0]):
for y in range(blit.size[1]):
data = []
alpha = fg_data[x,y][3]/255.0
for c in range(3):
lhs = bg_data[x,y][c]
rhs = int(fg_data[x,y][c] * alpha)
data.append(min(lhs+rhs, 255))
data.append(1)
out_data[x,y] = tuple(data)
return out
def add_actors(out_img, actors):
for actor in actors:
if actor.image:
img_path = actor.image
img = Image.open(img_path).convert("RGBA")
shape = actor.clip
sprite = img.crop(make_box(*shape))
if actor.effect:
apply_effect(sprite, actor.effect)
x = actor.x * TILE_SIZE
y = actor.y * TILE_SIZE
paste_shape = [x, y] + list(sprite.size)
if actor.zoom:
new_x, new_y, new_w, new_h, scale = actor.zoom
sprite = sprite.resize([new_w, new_h])
x = new_x * TILE_SIZE
y = new_y * TILE_SIZE
paste_shape = [x, y] + list(sprite.size)
paste_box = make_box(*paste_shape)
bg = out_img.crop(paste_box)
mixed = None
if actor.effect:
mixed = add_composite(bg, sprite)
else:
mixed = Image.alpha_composite(bg, sprite)
out_img.paste(mixed, paste_box)
def convert_to_png(level_path, tiles_path, sprites_path, out_path):
setup_paths(sprites_path, out_path)
level = load_level(level_path)
tiles = tile_segments(tiles_path)
out_img = generate_map(level.board, tiles)
layers = {
"normal" : [],
"light" : [],
}
for actor in level.actors:
if actor.layer < 2:
layers["normal"].append(actor)
else:
layers["light"].append(actor)
for baddy in level.baddies:
actor = baddy.make_fake_actor()
layers["normal"].append(actor)
add_actors(out_img, layers["normal"])
if len(level.effects) == 1:
apply_area_effect(out_img, level.effects[0])
elif len(level.effects) > 1:
print "multiple area lighting effects detected"
# TODO apply area lighting here
add_actors(out_img, layers["light"])
out_img.convert("RGB").save(out_path)
if __name__ == "__main__":
if len(sys.argv) == 1:
print "First argument must be a level file."
exit()
level_path = sys.argv[1]
tiles_path = os.path.join("sprites", "pics1.png")
if len(sys.argv) >= 3:
tiles_path = sys.argv[2]
level_name = os.path.split(level_path)[-1]
if len(sys.argv) >= 4:
level_name = sys.argv[3]
out_path = os.path.join("out", level_name + ".png")
for path in [level_path, tiles_path]:
if not os.path.isfile(path):
print "No such file: " + path
exit()
convert_to_png(level_path, tiles_path, "sprites", out_path)