-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.py
50 lines (37 loc) · 1.42 KB
/
sketch.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
shape = (size, size)
ranks = np.zeros(shape)
initial_white_noise = np.random.rand(size, size)
placed_pixels = initial_white_noise >= (1-initial_ratio)
count_placed = np.sum(placed_pixels)
count_remaining = placed_pixels.size - count_placed
prev_swap = None
# Phase 1: Place intial
while True:
blurred = gaussian(placed_pixels)
densest = (blurred * placed_pixels).argmax()
voidest = (blurred + placed_pixels).argmin()
if prev_swap == (voidest, densest):
break
if densest == voidest:
break
densest_coord = np.unravel_index(densest, shape)
voidest_coord = np.unravel_index(voidest, shape)
placed_pixels[densest_coord] = False
placed_pixels[voidest_coord] = True
prev_swap = (densest, voidest)
# Phase 2: Rank pixels by density
placed_but_not_ranked = placed_pixels.copy()
for rank in range(count_placed, 0, -1):
blurred = gaussian(placed_but_not_ranked)
densest = (blurred * placed_but_not_ranked).argmax()
densest_coord = np.unravel_index(densest, shape)
placed_but_not_ranked[densest_coord] = False
ranks[densest_coord] = rank
# Phase 3: Fill up remaining pixels from the sparsest areas
for rank in range(count_remaining):
blurred = gaussian(placed_pixels)
voidest = (blurred + placed_pixels).argmin()
voidest_coord = np.unravel_index(voidest, shape)
placed_pixels[voidest_coord] = True
ranks[voidest_coord] = count_placed + rank
result = ranks