-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaveragehue.py
executable file
·74 lines (64 loc) · 2.16 KB
/
averagehue.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
#!/usr/bin/python
import sys
import math
from PIL import Image
import colorsys
import numpy as np
from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
def hue(r,g,b):
''' Ganked from colorsys.rgb_to_hsv'''
maxc = max(r, g, b) / 255.0
minc = min(r, g, b) / 255.0
if minc == maxc:
return 0.0
rc = (maxc-r) / (maxc-minc)
gc = (maxc-g) / (maxc-minc)
bc = (maxc-b) / (maxc-minc)
if r == maxc:
h = bc-gc
elif g == maxc:
h = 2.0+rc-bc
else:
h = 4.0+gc-rc
h = (h/6.0) % 1.0
return h
rgb_to_hue = np.vectorize(hue)
def circular_mean(angles):
''' Given a series of angles in radians, return the mean of circular quantities
See: http://en.wikipedia.org/wiki/Mean_of_circular_quantities'''
n = len(angles)
mean = math.atan2(sum([math.sin(a) for a in angles]) / float(n), sum([math.cos(a) for a in angles]) / float(n))
#print mean
return mean
circular_average = lambda x, y: (x + y) / 2 if abs(x-y) < 0.5 else ((x + y - 1) / 2) % 1.0
def hue_array(image):
# get pixels
arr = np.array(image.convert('RGBA').resize((200,200)))
r, g, b, a = np.rollaxis(arr, axis=-1)
# get hue of each
h = rgb_to_hue(r, g, b)
# print h.shape
# print h[0]
# convert from 0..1 scale to 0..2pi (radians)
#h *= 2 * math.pi
# print h[0]
return h.flatten()
def reducer(l):
pairs = l
while len(pairs)>1:
pairs = [circular_average(pairs[i*2],pairs[i*2+1]) for i in xrange(len(pairs)/2)]
return pairs[0]
if __name__ == '__main__':
#print "Getting file list"
files = sys.argv[1:]
#result = [(circular_mean(hue_array(Image.open(file))), file) for file in files]
#print int((result[0][0]+math.pi)*50)
#result = [(reduce(circular_average,sorted(hue_array(Image.open(file)))), file) for file in files]
sorted_hue_array = lambda file: sorted(hue_array(Image.open(file)))
result = [(reducer(sorted_hue_array(file)), file) for file in files]
print "%03d" % int(result[0][0]*500)
#print sorted(result)