from collections import Counter
image = [[0,0,64,64],[0,64,128,128],[128,192,255,255],[192,192,255,0]]
values = [x for row in image for x in row]
def histogram(xs):
    if not xs or any(type(x) is not int or not 0 <= x <= 255 for x in xs):
        raise ValueError('nonempty 8-bit integer pixels required')
    counts = Counter(x//64 for x in xs)
    return [counts[i] for i in range(4)]
counts = histogram(values)
print('counts', counts)
print('fractions', [n/len(values) for n in counts])
print('mean', sum(values)/len(values))
assert counts == [4,3,3,6]
assert histogram(list(reversed(values))) == counts
assert histogram([255]) == [0,0,0,1]
assert sum(counts) == 16
