-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdithering.py
46 lines (40 loc) · 1.52 KB
/
dithering.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
import numpy as np
THRESHOLD_MAP = (1 / 16) * np.array([
[0, 8, 2, 10],
[12, 4, 14, 6],
[3, 11, 1, 9],
[15, 7, 13, 5]
])
def floyd_steinberg_dithering(img_arr):
h, w = img_arr.shape
# output = np.copy(img_arr) + np.random.random_sample([h, w])
output = np.copy(img_arr)
for j in range(h):
for i in range(w):
x = i if j % 2 == 0 else w - 1 - i
original_pixel = output[j][x]
# new_pixel = find_closest_color(original_pixel, palette)
new_pixel = round(original_pixel)
output[j][x] = new_pixel
error = original_pixel - new_pixel
if j < h - 1 and 0 < x < w - 1 and j % 2 == 0:
output[j][x + 1] += error * 7 / 16
output[j + 1][x - 1] += error * 3 / 16
output[j + 1][x] += error * 5 / 16
output[j + 1][x + 1] += error * 1 / 16
if j < h - 1 and 0 < x < w - 1 and j % 2 == 1:
output[j][x - 1] += error * 7 / 16
output[j + 1][x - 1] += error * 3 / 16
output[j + 1][x] += error * 5 / 16
output[j + 1][x - 1] += error * 1 / 16
return (np.clip(output, 0, 1) * 255).astype(np.uint8)
def ordered_dithering(img_arr):
h, w = img_arr.shape
output = np.zeros([h, w], dtype=bool)
for j in range(h):
for i in range(w):
if img_arr[j][i] <= THRESHOLD_MAP[j % 4][i % 4]:
output[j][i] = 0
else:
output[j][i] = 1
return output