-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolour_palette.py
84 lines (61 loc) · 2.46 KB
/
colour_palette.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
import requests
import json
import numpy as np
import pandas as pd
from unsplash_keys import access_key
from imageio import imread
from skimage.transform import resize
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from colorit import *
_NUMERALS = '0123456789abcdefABCDEF'
_HEXDEC = {v: int(v, 16) for v in (x+y for x in _NUMERALS for y in _NUMERALS)}
LOWERCASE, UPPERCASE = 'x', 'X'
def triplet(rgb, lettercase=LOWERCASE):
return format(rgb[0]<<16 | rgb[1]<<8 | rgb[2], '06'+lettercase)
def getRandomImageLink(query = []) -> str:
query_string = "&query="
for i in range(len(query)):
query_string += query[i]
if i != (len(query) - 1):
query_string += ","
request_string = "https://api.unsplash.com/photos/random/?client_id="+access_key+""+query_string
r = requests.get(request_string)
x = r.json()
link = (x["urls"]["raw"])
return link
def preProcessImageReturnDataframe(dimensions = (300,300), query = []):
image_link = getRandomImageLink(query=query)
img = imread(image_link)
img = resize(img, dimensions)
data = pd.DataFrame(img.reshape(-1,3), columns = ["Red","Green","Blue"])
return data
def getKMeansModel(num_clusters = 3, random_state = 42):
kmeans_model = KMeans(
n_clusters= num_clusters,
random_state= random_state
)
return kmeans_model
def getHexValues(num_clusters = 3, random_state = 42,dimensions = (300,300), query=[]) -> list:
df = preProcessImageReturnDataframe(dimensions=dimensions, query=query)
model = getKMeansModel(num_clusters=num_clusters,random_state=random_state)
cluster_list = model.fit_predict(df)
cluster_centers = model.cluster_centers_
list_of_colours = []
for i in cluster_centers:
temp_list = [int(x*255) for x in i]
list_of_colours.append(tuple(temp_list))
list_of_hex_values = []
for i in list_of_colours:
list_of_hex_values.append(triplet(i,UPPERCASE))
return list_of_colours, list_of_hex_values
if __name__ == "__main__":
print("How many clusters do you want?")
num_clusters = int(input())
print("Enter your search parameters, separated by commas")
param_string = str(input())
query = param_string.split(",")
listRGB, listHex = getHexValues(num_clusters = num_clusters, random_state = 5, query=query)
init_colorit()
for i in range(len(listRGB)):
print(background(listHex[i],listRGB[i]))