cross-stitch

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

commit 35a43e65034c9ad4fbd06132c518970b3cac7c2a
parent 503df807a1d7659850deca687081e91fb672272e
Author: Anders Damsgaard <anders.damsgaard@geo.au.dk>
Date:   Sat, 15 Feb 2014 21:31:49 +0100

256 color palette works, but is too slow

Diffstat:
Mcross-stitch.py | 35++++++++++++++++++++++-------------
1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/cross-stitch.py b/cross-stitch.py @@ -18,11 +18,12 @@ class Palette: def __init__(self, type='256colors'): if type == '256colors': self.rgblist = numpy.loadtxt('./256-color-rgb.dat') + print(self.rgblist.shape) def nearest256color(self, rgbval): ibest = -1 - min_misfit2 = float(inf) - for i in range(self.rgblist): + min_misfit2 = float('inf') + for i in range(256): palettecolor = self.rgblist[i] misfit2 = (rgbval[0] - float(palettecolor[0]))**2 + \ (rgbval[1] - float(palettecolor[1]))**2 + \ @@ -30,8 +31,8 @@ class Palette: if misfit2 < min_misfit2: ibest = i min_misfit2 = misfit2 - return numpy.array((palettecolor[ibest,0], palettecolor[ibest,1], - palettecolor[ibest,2])) + return numpy.array((self.rgblist[ibest,0], self.rgblist[ibest,1], + self.rgblist[ibest,2])) class CrossStitch: @@ -62,6 +63,14 @@ class CrossStitch: tmp[scipy.r_[scipy.where(vecs == i)],:] = color self.img = tmp.reshape(self.img.shape[0], self.img.shape[1], 3) + def convert_256_colors(self): + palette = Palette('256colors') + tmp = self.img.reshape(scipy.product(self.img.shape[:2]),\ + self.img.shape[2]) + for i in range(tmp.size): + tmp[i] = palette.nearest256color(tmp[i]) + self.img = tmp.reshape(self.img.shape[0], self.img.shape[1], 3) + def save_image(self, filename, grid=True): fig = matplotlib.pyplot.figure() imgplot = matplotlib.pyplot.imshow(self.img, interpolation='nearest') @@ -72,15 +81,6 @@ class CrossStitch: return self.img -def ask(parent=None, message=''): - app = wx.App() - dlg = wx.TextEntryDialog(parent, message) - dlg.ShowModal() - result = dlg.GetValue() - dlg.Destroy() - app.MainLoop() - return result - class MainScreen(wx.Frame): def __init__(self, *args, **kwargs): @@ -122,6 +122,10 @@ class MainScreen(wx.Frame): fitem = processingMenu.Append(wx.ID_ANY, 'Reduce number of colors', 'Reduce number of colors in image') self.Bind(wx.EVT_MENU, self.OnLimitColors, fitem) + fitem = processingMenu.Append(wx.ID_ANY,\ + 'Reduce to standard 256 colors',\ + 'Reduce number of colors in image to the standard 256 colors') + self.Bind(wx.EVT_MENU, self.On256Colors, fitem) menubar.Append(processingMenu, '&Image processing') viewMenu = wx.Menu() @@ -223,6 +227,11 @@ class MainScreen(wx.Frame): self.contentNotSaved = True self.DrawPreview() + def On256Colors(self, event): + self.cs.convert_256_colors() + self.contentNotSaved = True + self.DrawPreview() + def ToggleGrid(self, event): if self.gridtoggle.IsChecked(): self.grid = True