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
| # list版 占13.9mb
def imageSmoother(self, img):
res = []
l1, l2 = len(img), len(img[0])
expand = np.pad(img, ((1, 1), (1, 1)), 'constant', constant_values=-1)
expand = expand.tolist()
res = [[0] * l2 for _ in range(l1)]
for i in range(l1):
for j in range(l2):
nine = [expand[i][j], expand[i][j + 1], expand[i][j + 2], expand[i + 1][j], expand[i + 1][j + 1],
expand[i + 1][j + 2], expand[i + 2][j], expand[i + 2][j + 1], expand[i + 2][j + 2]]
num = nine.count(-1)
res[i][j] = (sum(nine) + num) // (9 - num)
return res
# numpy版 占25.3mb
def imageSmoother(self, img):
import numpy as np
l1, l2 = len(img), len(img[0])
expand = np.pad(img, ((1, 1), (1, 1)), 'constant', constant_values=-1)
expand = expand.tolist()
res = [[0] * l2 for _ in range(l1)]
for i in range(l1):
for j in range(l2):
nine = [expand[i][j], expand[i][j + 1], expand[i][j + 2], expand[i + 1][j], expand[i + 1][j + 1],
expand[i + 1][j + 2], expand[i + 2][j], expand[i + 2][j + 1], expand[i + 2][j + 2]]
num = nine.count(-1)
res[i][j] = (sum(nine) + num) // (9 - num)
return res
|