aboutsummaryrefslogtreecommitdiff
path: root/config/ratpoison/py/colorz.py
diff options
context:
space:
mode:
authorVasil Zlatanov <vasil.zlatanov@gmail.com>2015-08-18 02:38:33 +0200
committerVasil Zlatanov <vasil.zlatanov@gmail.com>2015-08-18 02:38:33 +0200
commit6efac1a12c8ab0b7d96a8d342fbd74fce35e8c42 (patch)
tree93ebfb332b3a231931fe1b1276632bdaa33fa6e4 /config/ratpoison/py/colorz.py
parentc021038355b6773ab13dae92327fda50c366395f (diff)
downloaddotfiles-6efac1a12c8ab0b7d96a8d342fbd74fce35e8c42.tar.gz
dotfiles-6efac1a12c8ab0b7d96a8d342fbd74fce35e8c42.tar.bz2
dotfiles-6efac1a12c8ab0b7d96a8d342fbd74fce35e8c42.zip
Organise everything systematically.
Diffstat (limited to 'config/ratpoison/py/colorz.py')
-rw-r--r--config/ratpoison/py/colorz.py71
1 files changed, 0 insertions, 71 deletions
diff --git a/config/ratpoison/py/colorz.py b/config/ratpoison/py/colorz.py
deleted file mode 100644
index 8c00f0c..0000000
--- a/config/ratpoison/py/colorz.py
+++ /dev/null
@@ -1,71 +0,0 @@
-from collections import namedtuple
-from math import sqrt
-import random
-try:
- import Image
-except ImportError:
- from PIL import Image
-
-Point = namedtuple('Point', ('coords', 'n', 'ct'))
-Cluster = namedtuple('Cluster', ('points', 'center', 'n'))
-
-def get_points(img):
- points = []
- w, h = img.size
- for count, color in img.getcolors(w * h):
- points.append(Point(color, 3, count))
- return points
-
-rtoh = lambda rgb: '#%s' % ''.join(('%02x' % p for p in rgb))
-
-def colorz(filename, n=3):
- img = Image.open(filename)
- img.thumbnail((200, 200))
- w, h = img.size
-
- points = get_points(img)
- clusters = kmeans(points, n, 1)
- rgbs = [map(int, c.center.coords) for c in clusters]
- return map(rtoh, rgbs)
-
-def euclidean(p1, p2):
- return sqrt(sum([
- (p1.coords[i] - p2.coords[i]) ** 2 for i in range(p1.n)
- ]))
-
-def calculate_center(points, n):
- vals = [0.0 for i in range(n)]
- plen = 0
- for p in points:
- plen += p.ct
- for i in range(n):
- vals[i] += (p.coords[i] * p.ct)
- return Point([(v / plen) for v in vals], n, 1)
-
-def kmeans(points, k, min_diff):
- clusters = [Cluster([p], p, p.n) for p in random.sample(points, k)]
-
- while 1:
- plists = [[] for i in range(k)]
-
- for p in points:
- smallest_distance = float('Inf')
- for i in range(k):
- distance = euclidean(p, clusters[i].center)
- if distance < smallest_distance:
- smallest_distance = distance
- idx = i
- plists[idx].append(p)
-
- diff = 0
- for i in range(k):
- old = clusters[i]
- center = calculate_center(plists[i], old.n)
- new = Cluster(plists[i], center, old.n)
- clusters[i] = new
- diff = max(diff, euclidean(old.center, new.center))
-
- if diff < min_diff:
- break
-
- return clusters