-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradients.py
127 lines (116 loc) · 3.9 KB
/
gradients.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env
# -*- coding: utf-8 -*-
"""
Defines color gradients and gives the ability to apply them.
"""
try:
# first try to use bohrium, since it could help us accelerate
# https://bohrium.readthedocs.io/users/python/
import bohrium as np
except ImportError:
# if not, plain old numpy is good enough
import numpy as np
from .colors import *
from .imageRepr import *
from .colorSpaces import grayscale
GRADIENT_BLACK_TO_WHITE=[(0.0,(0.0,0.0,0.0)),(1.0,(1.0,1.0,1.0))]
GRADIENT_CLEAR_TO_WHITE=[(0.0,(1.0,1.0,1.0,0.0)),(1.0,(1.0,1.0,1.0,1.0))]
GRADIENT_CLEAR_TO_BLACK=[(0.0,(0.0,0.0,0.0,0.0)),(1.0,(0.0,0.0,0.0,1.0))]
GRADIENT_RAINBOW=[
(0.0,(1.0,0.0,0.0)),
(0.2,(1.0,0.75,0.0)),
(0.4,(1.0,1.0,0.0)),
(0.6,(0.0,1.0,0.0)),
(0.8,(0.0,0.0,1.0)),
(1.0,(0.8,0.0,1.0))]
def colormap(img,colors=None):
"""
apply the colors to a grayscale image
if a color image is provided, convert it (thus, acts like a "colorize" function)
:param img: a grayscale image
:param colors: [(decimalPercent,color),(...)]
if no colors are given, then [(0.0,black),(1.0,white)]
if a single color and no percent is given, assume [(0.0,black),(0.5,theColor),(1.0,white)]
:return: the resulting image
"""
img=grayscale(img)
if not isFloat(img):
img=img/255.0
if colors is None:
colors=[(0.0,(0.0,0.0,0.0)),(1.0,(1.0,1.0,1.0))]
elif not isinstance(colors[0],(tuple,list,np.ndarray)):
white=[]
black=[]
if isinstance(colors,str):
colors=strToColor(colors)
if isFloat(colors):
imax=1.0
imin=0.0
else:
imax=255
imin=0
for _ in range(len(colors)):
white.append(imax)
black.append(imin)
if len(colors) in [2,4]: # keep same alpha value
black[-1]=colors[-1]
white[-1]=white[-1]
colors=[(0.0,black),(0.5,colors),(1.0,white)]
else:
colors.sort() # make sure we go from low to high
# make sure colors are in the shape we need
colors=[[matchColorToImage(color[0],img),np.array(strToColor(color[1]))] for color in colors]
shape=(img.shape[0],img.shape[1],len(color[1]))
img2=np.ndarray(shape)
img=img[...,None]
if True:
lastColor=None
for color in colors:
if lastColor is None:
img2+=color[1]
else:
percent=(img-lastColor[0])*lastColor[0]/color[0]
img2=np.where(
np.logical_and(img>lastColor[0],img<=color[0]),
(color[1]*percent)+(lastColor[1]*(1-percent)),img2)
lastColor=color
img2=np.where(img>lastColor[0],lastColor[1],img2)
else:
def gradMap(c):
lastColor=None
for color in colors:
if c<color[0]:
if lastColor is None:
return color[1]
percent=(c-lastColor[0])/color[0]
return (lastColor[1]*percent+color[1])/(2*percent)
lastColor=color
return lastColor[1]
img2=perPixel(gradMap,img,clamp=False)
return img2
def cmdline(args):
"""
Run the command line
:param args: command line arguments (WITHOUT the filename)
"""
printhelp=False
if not args:
printhelp=True
else:
for arg in args:
if arg.startswith('-'):
arg=[a.strip() for a in arg.split('=',1)]
if arg[0] in ['-h','--help']:
printhelp=True
else:
print('ERR: unknown argument "'+arg[0]+'"')
else:
print('ERR: unknown argument "'+arg+'"')
if printhelp:
print('Usage:')
print(' gradients.py [options]')
print('Options:')
print(' NONE')
if __name__=='__main__':
import sys
cmdline(sys.argv[1:])