Skip to content

Instantly share code, notes, and snippets.

@itdaniher
Created January 6, 2018 04:14
Show Gist options
  • Save itdaniher/0424c2339ff24eda40a8b33c0ab59008 to your computer and use it in GitHub Desktop.
Save itdaniher/0424c2339ff24eda40a8b33c0ab59008 to your computer and use it in GitHub Desktop.
imfux.py
import matplotlib
import numpy as np
import phase1
from scipy import misc
im_raw = misc.imread('image2.png')
nearest = lambda array, value: array[(np.abs(array-value)).argmin()]
im_flat = im_raw.reshape(im_raw.shape[0]*im_raw.shape[1], im_raw.shape[2])
colours_top = [x[1] for x in sorted(phase1.rle(sorted([tuple(x) for x in im_flat], reverse=True)), reverse=True)][:4]
colours_list = [tuple(x) for x in colours_top]
im_top_flat = np.array([nearest(colours_top, colour) if tuple(colour) not in colours_list else colour for colour in im_flat])
im_top_flat_bw = np.array([x[0] for x in (im_top_flat > np.mean(colours_top))])
im_top = im_top_flat.reshape(im_raw.shape)
im_top_bw = im_top_flat_bw.reshape(im_raw.shape[0], im_raw.shape[1])
misc.imsave('image3.png', im_top)
misc.imsave('image3.bw.png', im_top_bw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment