Skip to content

Instantly share code, notes, and snippets.

@arosh
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arosh/9362385 to your computer and use it in GitHub Desktop.
Save arosh/9362385 to your computer and use it in GitHub Desktop.
# coding: utf-8
import sys
import numpy
from scipy.misc import imread, imsave
from scipy.cluster.vq import kmeans2
def main():
if len(sys.argv) == 2:
ifname = sys.argv[1]
else:
ifname = './shinchoku.jpg'
k = 31
src_image = imread(ifname)
dots = src_image.reshape((src_image.shape[0]*src_image.shape[1], src_image.shape[2]))
centroid, label = kmeans2(dots, k)
centroid = centroid.astype(numpy.uint8)
dst_image = centroid[label]
dst_image = dst_image.reshape((src_image.shape[0], src_image.shape[1], src_image.shape[2]))
imsave('dst_image.bmp', dst_image)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment