-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorSchemer.py
403 lines (273 loc) · 13.1 KB
/
colorSchemer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#"I hereby certify that this program is solely the result of my own work and is in compliance with the Academic Integrity policy of the course syllabus and the academic integrity policy of the CS department.”
import random
import Draw
import math
# global variables
# the canvas width and height
canvasW = 686
canvasH = 600
# function purpose: finds the Euclidean distance between two color points by their
# rgb values
# function returns: the Euclidean distance between the two color points
def pixDist(r1,g1,b1,r2,g2,b2):
# returns the euclidean distance btwn the 2 rgb values
return int((r1-r2)**2 + (g1-g2)**2 + (b1-b2)**2)
# function purpose: uses a clustering algorithm to create groups of the "closest"
# pixels and modifies the centroid, or average pixel value,
# with each new addition to each cluster
# function returns: None
def clusterize(allPix,centroids,clusters):
# weighting colors that make up over 10% of the image less
# initializing vars
cutPix = []
dict = {}
# loopinng through all the pixels
for p in allPix:
# creating a dictionary with all the pixels and the number of times
# they appear in allPix
if p in dict:
dict[p]+=1
else:
dict[p] =1
# the cutOff for a "large" amount of a color is if it makes up about over
# 20% of all the pixels
cutOff = int(len(allPix)*(20/100))
for k in dict:
# if the count of the pixel is greater than the cutoff
if(dict[k] > cutOff):
# add that pixel 2% less than it appears in allPix, or in other words
# only include 98% of the iterations of that pixel
for i in range(int(0.98*dict[k])): cutPix.append(k)
else:
# otherwise add it the original amount of times it appears in allPix
for i in range(dict[k]): cutPix.append(k)
# loop through all the pixels
for p in cutPix:
# we will start with the max distance and max pix values
minDist = pixDist(255,255,255,0,0,0)
for c in range(len(centroids)):
r1,g1,b1 = centroids[c]
r2,g2,b2 = p
# find the pixels distance to the current centroid
dist = pixDist(r1,g1,b1,r2,g2,b2)
# if the pix distance from the centroid < the min distance btwn
# that pix and the other centroids then make that dist the minDist
# and that centroid the closest centroid
if(dist < minDist):
minDist = dist
index = c # represents index of cluster list OR index of which centroid closest to
# add that pixel to the closest centroid
clusters[index].append(p)
# change the centroid to average together
r1,g1,b1 = centroids[index]
r2,g2,b2 = p
# taking avg of old centroid and new cluster addition to
# get new centroid
centroids[index] = (r1+r2)//2,(g1+g2)//2,(b1+b2)//2
# function purpose: sorts the clusters from darkest (closest to (255,255,255))
# to lightest (closest to (0,0,0)
# function returns: a new list with the sorted centroids
def sortedDL(centroids):
# getting a list of tuples with each centroid's index
# and each centroid's Euclidean distance from Black (255,255,255)
distFromBlack = [(i,pixDist(centroids[i][0],centroids[i][1],centroids[i][2],255,255,255)) for i in range(len(centroids))]
# sorting the list created above by the second element which is the distance
# from black (255,255,255)
distFromBlack = sorted(distFromBlack, key = lambda x: x[1])
# return a list of the centroids ordered by their distance from black
# the order is stored in the above list distFromBlack
return [centroids[i[0]] for i in distFromBlack]
# function purpose: reads in a color names dataset and matches each rgb value
# in the centroids list to their closest color names
# function returns: a new list of color names that corresponds to the inputted
# centroids list
def getColorName(centroids):
# dictionary with centroid as key and a tuple of the closest color name as
# the data
centroidNames = []
# creating an empty 2d list
data = {}
# load in the data
fin = open("colorNames.csv")
# reading the first row of headers
fin.readline()
for line in fin:
# getting a line from the file
dat = line.strip()
dat = line.split(",")
# the color name is the 0th element of the line
name = dat[0]
# rgb values are dat[1],dat[2],and dat[3] respectively
rgb = (dat[1],dat[2],dat[3])
# add to the data dictionary
data[name] = rgb
fin.close()
for c in centroids:
# set minDist to the maximum dist possible
minDist = pixDist(0,0,0,255,255,255)
# closest colorName
closestName = ""
# looping through the color name data
for k in data:
r,g,b = c # centroid rgb vals
r1,g1,b1 = data[k] # corresponding rgb vals to the key
dist = pixDist(r,g,b,int(r1),int(g1),int(b1))
# if the distance is less than the minDist
# the values at that color become the values of the current pix
if(dist < minDist):
closestName = k
minDist = dist
centroidNames+=[closestName]
return centroidNames
# function purpose: finds the hex values of a pixel based off its rgb values
# function returns: a list of hex values corresponding to the inputted list
# of centroids
def getHex(centroids):
hexVals = []
decToHex = {0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:"A",11:"B",
12:"C",13:"D",14:"E",15:"F"}
for c in centroids:
# hex values start with a #
hexStr = "#"
for i in range(len(c)):
# the first hex digit is the hex equivalent of the int division by 16
hexDig1 = decToHex[c[i]//16]
#print("dig1",hexDig1)
# the second hex digit is the hex equivalent of the remainder from
# the first hex dig times 16
hexDig2 = decToHex[int(abs(c[i]//16 - c[i]/16)*16)]
# add the first and second dig to the hexstring
hexStr+=(str(hexDig1)+str(hexDig2))
hexVals+=[hexStr]
return hexVals
# function purpose: Draws the picture in a centered and aesthetically pleasing manner
# function returns: None
def drawPic(path, picW, picH, rectX, rectY, rectW, rectH, centroids):
# we want to draw the picture centered in our frame
# initialize values as if the picture is as big as the rectange or frame
picX = rectX
picY = rectY
# if the pic width is less than the rect width
if(picW < rectW):
# then add the error//2 to the pic's x coord
picX += (rectW - picW)//2
# if the pic height is less than the rect height
if(picH < rectH):
# then add the error//2 to the pic's y coord
picY += (rectH - picH)//2
# draw the picture
Draw.picture(path,picX,picY)
# draw white border around picture
Draw.setColor(Draw.WHITE)
Draw.rect(picX,picY,picW,picH)
# function purpose: Draws the color boxes with their names, hex values, and rgb values
# function returns: None
def drawColors(centroids,centroidNames,centroidHex, rectH, rectW, rectX, rectY):
colorsH = 60
colorsW = rectW//len(centroids)
colorsStartX = rectX
colorsY = rectY + (rectH-colorsH)
wordsH = 45
# drawing a white space for the text
Draw.setColor(Draw.WHITE)
Draw.filledRect(rectX,colorsY-wordsH,rectW,wordsH)
for i in range(len(centroids)):
# set color to values in current centroid
Draw.setColor(Draw.color(centroids[i][0],centroids[i][1],centroids[i][2]))
colorsX = colorsStartX + i*colorsW
Draw.filledRect(colorsX,colorsY,colorsW,colorsH)
# draw black line separating white space above colors
Draw.setColor(Draw.BLACK)
Draw.line(colorsX,colorsY-wordsH,colorsX,colorsY+colorsH)
# draw the words text: hex and color names
Draw.setColor(Draw.BLACK)
# drawing hex and color names
# colorsX+1 so word isn't on line/ for aesthetic reasons
Draw.setFontSize(8)
Draw.string(centroidNames[i],colorsX+1,colorsY-wordsH)
Draw.setFontSize(8)
Draw.string(centroidHex[i],colorsX+1,colorsY-2*(wordsH//3))
Draw.setFontSize(8)
rgb = "(" + str(centroids[i][0]) + ", " + str(centroids[i][1]) + ", " + str(centroids[i][2]) + ")"
Draw.string(rgb,colorsX+1,colorsY-(wordsH//3))
# draw black border
Draw.setColor(Draw.BLACK)
Draw.rect(rectX,rectY,rectW,rectH)
Draw.rect(rectX,colorsY-wordsH,rectW,wordsH)
# function purpose: Draws the color schemer logo
# function returns: None
def drawLogo(logoX,logoY):
Draw.setFontFamily("Broadway")
Draw.setFontSize(30)
Draw.setColor(Draw.BLACK)
Draw.filledRect(logoX,logoY,425,50)
Draw.setColor(Draw.WHITE)
Draw.string("The Color Schemer", logoX+10, logoY)
# function purpose: Draws the final picture
# function returns: None
def drawPalette(path, centroids, picW, picH, centroidNames, centroidHex):
# set values for our frame
rectW = 664 # must be multiple of 8 since we have 8 centroids
rectH = 460
rectX = (canvasW - rectW)//2 # since we want equal margins we divide the difference by 2
rectY = 80
# setting background of frame to color to the darkest color of centroids
clen = len(centroids)
Draw.setColor(Draw.color(centroids[0][0],centroids[0][1],centroids[0][2]))
# draw the frame
Draw.filledRect(rectX,rectY,rectW,rectH)
# draw the picture
drawPic(path,picW, picH, rectX, rectY, rectW, rectH-50, centroids)
# to cover the side where the image goes over the frame on the right
if(picW > rectW):
coverX = rectX + rectW # x will be where the width of the rect ends on the right
coverY = rectY # y is the same y as the rectangle
coverW = canvasW - rectX - rectW # the width is the canvas width,
# minus the rectangle x minus its width
coverH = canvasH
Draw.setColor(Draw.WHITE)
Draw.filledRect(coverX,coverY,coverW,canvasH)
# draw the colored boxes from the centroids
drawColors(centroids,centroidNames,centroidHex, rectH, rectW, rectX, rectY)
# draw the color schemer logo
drawLogo(rectX, rectY)
# function purpose: Function that puts everything together, finding
# the centroids and then drawing the final picture
# function returns: None
def colorSchemer(path):
#setting the canvas size to:
Draw.setCanvasSize(canvasW,canvasH)
width,height = Draw.getPictureSize(path)
allPix = []
for w in range(width):
for h in range(height):
cpix = Draw.getPixel(path,w,h)
allPix.append(cpix)
# selecting 8 unique random centroids (in form of indeces) to begin
centroids = random.sample(allPix,8)
print("old centroids:" + str(centroids))
#clusters list
clusters = [[c] for c in centroids]
#remove centroids since they already clustered
allPix = [i for i in allPix if i not in centroids]
# print("original cluster len",[len(c) for c in clusters])
# clusterize the pixels by centroid
clusterize(allPix,centroids,clusters)
print("new centroids:" + str(centroids))
# sort the centroids from darkets Black = (0,0,0) to lightest White = (255,255,255)
centroids = sortedDL(centroids)
# get lists of the hex values and the closest names
centroidNames = getColorName(centroids)
centroidHex = getHex(centroids)
# draw the final picture
drawPalette(path, centroids, width, height, centroidNames, centroidHex)
def main():
colorSchemer("pics/itWorked.gif") # shows a human subject
#colorSchemer("pics/rainbow.gif") # shows many colors
#colorSchemer("pics/galaxy.gif") # shows similar colors
#colorSchemer("pics/pinkClouds.gif") # shows similar colors
#colorSchemer("pics/ocean.gif") # shows similar colors
#colorSchemer("pics/flower.gif") # shows a colorful subject (flower)
#colorSchemer("pics/aaronJudge.gif") # shows a human subject
#colorSchemer("pics/stickFigures.gif") # shows many colors on a plain background
main()