def discretize(chan_r, chan_g, chan_b, n=50): """Split the list down into blocks and calculate their mean. Results in a smaller (original_len / n) list with approximated values. :param chan_r: Iterable of the red channel :param chan_g: Iterable of the green channel :param chan_b: Iterable of the blue channel :param n: How big the block size shall be. :returns: A generator that yields the new list lazily. """ group_r = grouper(chan_r, n) group_g = grouper(chan_g, n) group_b = grouper(chan_b, n) mean = lambda group: sum(group) / n for red, green, blue in zip(group_r, group_g, group_b): yield mean(red), mean(green), mean(blue)
def read_moodbar_values(path): """Read a vector of RGB triples from a mood-file (as produced by moodbar). :param path: The path where the mood file is located. :returns: A list of 1000 RGB Triples. """ with open(path, 'rb') as f: return [tuple(rgb) for rgb in grouper(f.read(), n=3)]
def read_moodbar_values(path): rgb_values = deque() with open(path, 'rb') as handle: vector = handle.read() if len(vector) is 0: raise OSError('broken moodbar file: ' + path) for rgb in grouper(vector, n=3): rgb_values.append(tuple(c / 0xff for c in rgb)) return list(rgb_values)