-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
40 lines (33 loc) · 924 Bytes
/
grid.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
"""
Misc utilities for working with square grid.
"""
import numpy as np
def sum_8(src):
"""
Return numpy array containing the sum of 8-neighbours from the source
array.
"""
dst = np.zeros_like(src)
dst[1:, :] += src[:-1, :]
dst[:-1, :] += src[1:, :]
dst[:, 1:] += src[:, :-1]
dst[:, :-1] += src[:, 1:]
dst[1:, 1:] += src[:-1, :-1]
dst[1:, :-1] += src[:-1, 1:]
dst[:-1, 1:] += src[1:, :-1]
dst[:-1, :-1] += src[1:, 1:]
return dst
def nonzero_8(src):
"""
Return numpy array containing the number of non-zero 8-neighbours from the
source array.
"""
return sum_8((src != 0) * 1.0)
def color_with(src, colors, background, palette):
"""
Using color palette, fill colors array according to color indices from the
source array.
"""
colors[:] = background
for idx, color in palette.items():
colors[src == idx] = color