cross-stitch

Interactively turn images into patterns for cross stitching
git clone git://src.adamsgaard.dk/cross-stitch
Log | Files | Refs

commit b8948bc4acd8107a723b6c23b5b417120b7813d9
parent 38af9a0714c260b0ed50af24ce635a17d301c87c
Author: Anders Damsgaard <anders.damsgaard@geo.au.dk>
Date:   Fri, 14 Feb 2014 15:17:59 +0100

added k-means clustering to reduce the number of colors

Diffstat:
MREADME.rst | 3+++
Mcross-stitch.py | 16++++++++++++----
Mfisker-pattern.png | 0
3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/README.rst b/README.rst @@ -30,6 +30,7 @@ Usage ----- usage: cross-stitch.py [-h] --infile file --outfile file [--width WIDTH] + [--ncolors NCOLORS] Downsamples and modifies an image in order to create a pattern for cross stitching. @@ -42,6 +43,8 @@ optional arguments: save processed image as file --width WIDTH, -w WIDTH canvas width, default value = 20 + --ncolors NCOLORS, -c NCOLORS + number of colors in output image, default value = 10 Example ------- diff --git a/cross-stitch.py b/cross-stitch.py @@ -4,6 +4,7 @@ import sys import argparse import scipy.ndimage import scipy.misc +import scipy.cluster import numpy as np import matplotlib.pyplot as plt @@ -19,12 +20,13 @@ parser.add_argument('--outfile', '-o', metavar='file', type=str, nargs=1, required=True, help='save processed image as file') parser.add_argument('--width', '-w', type=int, nargs=1, default=20, help='canvas width, default value = 20') -#parser.add_argument('--n-colors', '-c', type=int, nargs=1, default=10, - #help='number of colors in output image, default value = 10') +parser.add_argument('--ncolors', '-c', type=int, nargs=1, default=10, + help='number of colors in output image, default value = 10') args = parser.parse_args() infile = args.infile[0] outfile = args.outfile[0] width = args.width[0] +ncolors = args.ncolors ## Read image ################################################################## try: @@ -39,11 +41,17 @@ new_size = (int(round(hw_ratio*width)), width) im_small = scipy.misc.imresize(im, new_size) ## Process image colors ######################################################## - +ar = im_small.reshape(scipy.product(im_small.shape[:2]), im_small.shape[2]) +colors, dist = scipy.cluster.vq.kmeans(ar, ncolors) +c = ar.copy() +vecs, dist = scipy.cluster.vq.vq(ar, colors) +for i, color in enumerate(colors): + c[scipy.r_[scipy.where(vecs==i)],:] = color +im_small_reduced = c.reshape(new_size[0], new_size[1], 3) ## Generate output plot ######################################################## fig = plt.figure() -imgplot = plt.imshow(im_small) +imgplot = plt.imshow(im_small_reduced) imgplot.set_interpolation('nearest') plt.grid() plt.savefig(outfile) diff --git a/fisker-pattern.png b/fisker-pattern.png Binary files differ.