Update mprix and colors
This commit is contained in:
parent
b725dccade
commit
e1aa18dbed
20 changed files with 389 additions and 85 deletions
30
bin/dominant-color.py
Executable file
30
bin/dominant-color.py
Executable file
|
@ -0,0 +1,30 @@
|
|||
from __future__ import print_function
|
||||
import binascii
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import scipy
|
||||
import scipy.misc
|
||||
import scipy.cluster
|
||||
import sys
|
||||
|
||||
NUM_CLUSTERS = 5
|
||||
|
||||
# print('reading image')
|
||||
im = Image.open(sys.argv[1])
|
||||
im = im.resize((150, 150)) # optional, to reduce time
|
||||
ar = np.asarray(im)
|
||||
shape = ar.shape
|
||||
ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)
|
||||
|
||||
# print('finding clusters')
|
||||
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
|
||||
# print('cluster centres:\n', codes)
|
||||
|
||||
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
|
||||
counts, bins = np.histogram(vecs, len(codes)) # count occurrences
|
||||
|
||||
index_max = np.argmax(counts) # find most frequent
|
||||
peak = codes[index_max]
|
||||
colour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii')
|
||||
# print('most frequent is %s (#%s)' % (peak, colour))
|
||||
print(colour)
|
Loading…
Add table
Add a link
Reference in a new issue