[(30, 0), (21, 14), (7, 15), (1, 17), (1, 19), (33, 22), (3, 23), (1, 25),
(2, 26), (2, 28), (1, 29), (5, 30), (4, 32), (1, 34), (3, 36), (15, 38), (10, 39), ...,
, (1, 245), (2, 246), (5, 247), (4, 249), (4, 251), (5, 253), (2190, 255)]
>>> # 255-> White, 0-> Black
>>> # If we remove all the "whiter" colors
>>> im = im.point(lambda x: 255 if x>128 else x)
>>> # see how this policy works
>>> im.show()
↓
>>> # the new color distribution?
>>> im.getcolors()
...
>>> # new attempts
...
def clean(im): im = im.convert('L') im = im.point(lambda x:255 if x>128 or x==0 else x) im = im.point(lambda x:0 if x<255 else 255) return im
It's surprisingly simple, isn't it?
→
def im2array(im): return [ int(x!='\xff') for x in im.tobytes() ]
>>> # let's train a XOR operator
>>> import sklearn.svm
>>> clf = sklearn.svm.SVC()
>>> data = [(1, 1),
... (1, 0),
... (0, 1),
... (0, 0)]
>>> targets = [0, 1, 1, 0]
>>> clf.fit(data, targets)
>>> clf.predict((0, 0))[0]
1