-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortlock.py
52 lines (39 loc) · 1.31 KB
/
sortlock.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
import numpy as np
import matplotlib.colors as colors
import imageio
import random
BLOCK_SIZE = 8
# 0 for hue
# 1 for saturation
# 2 for value
SORTMETHOD = 1
def get_block_list(img, height, width):
res = []
for y in range(height):
for x in range(width):
res.append(img[y*BLOCK_SIZE:(y + 1)*BLOCK_SIZE,
x*BLOCK_SIZE:(x + 1)*BLOCK_SIZE, :])
return res
def block_list_to_img(block_list, height, width, sh, sw):
img = np.empty((sh, sw, 3), dtype=np.uint8)
for x in range(width):
for y in range(height):
block = block_list[y*width + x][1]
img[y*BLOCK_SIZE:(y + 1)*BLOCK_SIZE, x*BLOCK_SIZE:(x + 1)*BLOCK_SIZE, :] = block
return img
def magnitude(block):
hsv_block = colors.rgb_to_hsv(block)
return np.sum(hsv_block[:, :, SORTMETHOD])
def main():
img = np.array(imageio.imread_v2("/tmp/screen.png"))
h, w, _ = img.shape
width = w // BLOCK_SIZE
height = h // BLOCK_SIZE
block_list = get_block_list(img, height, width)
random.shuffle(block_list)
magnitudes = list(map(lambda b: (magnitude(b), b), block_list))
magnitudes.sort(key=lambda m: m[0])
new_img = block_list_to_img(magnitudes, height, width, h, w)
imageio.imwrite("/tmp/screen.png", new_img)
if __name__ == "__main__":
main()