-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbeautify.py
More file actions
48 lines (39 loc) · 1.28 KB
/
beautify.py
File metadata and controls
48 lines (39 loc) · 1.28 KB
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
import cv2
def toColour(img):
return cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
def beautify_grey_image(img):
return beautify_image(toColour(img))
def beautify_image(img):
colours = [[150., 0., 0.],
[0., 0., 0.],
[127., 127., 127.],
[255., 255., 255.]]
def colour_index(v):
return int(v*(len(colours)-1))
def value_for_index(index):
return float(index)/(len(colours)-1)
def convert(value):
v = float(value)/255.
if v > 0.999999:
v = 0.999999
if v < 0.01:
v = 0.
a = colours[colour_index(v)]
b = colours[colour_index(v)+1]
distance_to_base = abs(v - value_for_index(colour_index(v)))
b_factor = distance_to_base*(len(colours)-1)
a_factor = 1. - b_factor
if a_factor > 1. or a_factor < 0.:
sys.exit(0)
r = (a_factor*a[0] + b_factor*b[0])
g = (a_factor*a[1] + b_factor*b[1])
b = (a_factor*a[2] + b_factor*b[2])
return r, g, b
for row in range(0, img.shape[0]):
for col in range(0, img.shape[1]):
v = float(img[row, col, 2])
red, blue, green = convert(v)
img[row, col, 0] = blue
img[row, col, 1] = green
img[row, col, 2] = red
return img