-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux.py
177 lines (157 loc) · 5.5 KB
/
aux.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import csv
import cv2
import numpy as np
from PIL import Image
from sklearn.cluster import KMeans
# from sklearn.cluster import MiniBatchKMeans
def reshapeColor(colorEightBit):
'''
Returns a color triplet in the range of 0 to 1 from 0 to 255.
* I: List of 8-bit RGB components
* O: Normalized RBG components
'''
return [i / 255 for i in colorEightBit]
def upscaleColor(colorNormalized):
'''
Returns a color triplet of 0 to 255 from 0 to 1.
* I: Normalized list of RGB components
* O: List of 8-bit RGB components
'''
return [int(round(i * 255)) for i in colorNormalized]
def rgb_to_hex(rgb):
'''
Converts an RGB triplet to its hex equivalent.
* I: RGB 8-bit list
* O: Hex color string
'''
return '#xxx' % (int(rgb[0]), int(rgb[1]), int(rgb[2]))
def calcDominantColors(img, cltsNumb=10, maxIter=1000):
'''
Returns a tuple with the dominant colors arrays and their
clustering labels (sklearn).
* I:
-img: Image imported with cv2.imread
-clstNumb: Number of desired clusters
-maxIter: Maximum iterations number for detection
* O: Dominant colors arrays and clusters labels (sklearn)
'''
frame = img.reshape((img.shape[0] * img.shape[1], 3))
# Cluster the colors for dominance detection
kmeans = KMeans(n_clusters=cltsNumb, max_iter=maxIter).fit(frame)
(palette, labels) = (kmeans.cluster_centers_, kmeans.labels_)
# Rescale the colors for matplotlib
colors = [reshapeColor(color) for color in palette]
return (colors, labels)
def calcHexAndRGBFromPalette(palette):
'''
Returns the hex and RGB codes for the colors in a palette.
* I: Color palette
* O: Dictionary with hex and rgb colors
'''
sortedPalette = [upscaleColor(i) for i in palette]
(hexColors, rgbColors) = (
[rgb_to_hex(i) for i in sortedPalette],
sortedPalette
)
return {'hex': hexColors, 'rgb': rgbColors}
def genColorSwatch(img, heightProp, palette):
'''
Creates a color swatch that is proportional in height to the original
image (whilst being the same width).
* I:
-img: Image imported with cv2.imread
-heightProp: Desired height of the swatch in proportion to original img
-palette: Calculated palette through dominance detection
* O:
'''
clstNumber = len(palette)
(height, width, depth) = img.shape
pltAppend = np.zeros((round(height * heightProp), width, depth))
(wBlk, hBlk) = (round(width / clstNumber), round(height * heightProp))
for row in range(hBlk):
colorIter = -1
for col in range(width):
if (col % wBlk == 0) and (colorIter < clstNumber - 1):
colorIter = colorIter 1
pltAppend[row][col] = palette[colorIter]
return pltAppend * 255
def writeColorPalette(filepath, palette):
'''
Exports the HEX and RGB values of the palette to a tsv file.
* I:
-filepath: Path on disk to write the file to
-palette: Palette output of the getDominancePalette
* O:
-True
'''
with open(filepath, 'w') as csvfile:
wtr = csv.writer(csvfile, delimiter='\t')
for i in range(len(palette['hex'])):
wtr.writerow([
palette['hex'][i],
palette['rgb'][i]
])
return True
def genColorBar(width, height, color=[0, 0, 0], depth=3):
'''
Creates a solid color bar to act as visual buffer between frames rows.
* I:
-width: Desired width of the bar
-height: Desired height of the bar
-color: Desired color of the bar
-depth: Number of color components (3 for RGB)
* O:
-colorBar: Image with a solid color bar
'''
colorBar = np.full(
(height, width, depth),
color
)
return colorBar
def getDominancePalette(
imgPath,
clstNum=6,
maxIters=1000,
colorBarHeight=.03,
bufferHeight=.005,
colorBuffer=[0, 0, 0]
):
'''
Wrapper function that puts together all the elements to create the frame
with its swatch, and buffer bars.
* I:
-imgPath: Image location
-clstNum: Number of desired clusters
-maxIters: Maximum number of iterations for convergence
-colorBarHeight: Height of the color swatch (as proportion to img)
-bufferHeight: Height of the color buffer (as proportion to img)
-colorBuffer: Color of the buffer
* O:
-imgOut: Compiled image with swatch and buffers
-swatch: Color swatch
-palette: Hex and RGB color values
'''
# Load image
bgr = cv2.imread(imgPath)
img = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
(height, width, depth) = img.shape
# Cluster for dominance
(colors, labels) = calcDominantColors(
img, cltsNumb=clstNum, maxIter=maxIters
)
(hexColors, rgbColors) = calcHexAndRGBFromPalette(colors)
# Create color swatch
colorsBars = genColorSwatch(img, colorBarHeight, colors)
# Put the image back together
whiteBar = genColorBar(
width, round(height * bufferHeight), color=colorBuffer
)
newImg = np.row_stack((
whiteBar, colorsBars,
img,
colorsBars, whiteBar
))
palette = calcHexAndRGBFromPalette(colors)
swatch = Image.fromarray(colorsBars.astype('uint8'), 'RGB')
imgOut = Image.fromarray(newImg.astype('uint8'), 'RGB')
return (imgOut, swatch, palette)