From 0759ec41995574075d5a4e25d787edec47959c79 Mon Sep 17 00:00:00 2001 From: iou1name Date: Sun, 10 Dec 2017 14:13:36 -0500 Subject: [PATCH] nearestImage is better --- mosiac.py | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/mosiac.py b/mosiac.py index cbf395c..a8e0ce7 100755 --- a/mosiac.py +++ b/mosiac.py @@ -13,8 +13,10 @@ from PIL import Image class Mosiac(): def __init__(self): - self.imageBank = {} self.numClusters = 8 + self.tolerance = 0.5 + + self.imageBank = {} self.clusters = [] self.clusterMeans = [] @@ -96,16 +98,6 @@ class Mosiac(): for cluster in self.clusters: mean = tuple(np.mean(list(cluster.keys()), axis=(0))) self.clusterMeans.append(mean) - """ - indexs = list(range(len(self.images))) - step = len(self.images) // self.numClusters - self.clusters = [indexs[i*step : (i+1)*step] for i in - range(self.numClusters)] - for cluster in self.clusters: - means = [self.means[i] for i in cluster] - mean = tuple(np.mean(means, axis=(0))) - self.clusterMeans.append(mean) - """ print(f"initClusters took: {time.time()-then} seconds.") @@ -123,17 +115,12 @@ class Mosiac(): nodes = np.array(list(cluster.keys())) dist = np.sum((nodes - np.array(pixel))**2, axis=1) - # return np.argmin(dist) # closet value - dist.argsort()[:10] - return random.choice(list(dist)) - """ - dists = [] - for mean in cluster.keys(): - dist = np.linalg.norm(np.array(pixel)-np.array(mean)) - dists.append(dist) - """ - # choice = random.choice(sorted(dists)[:10]) - # return cluster[dists.index(choice)] + tol = int(len(dist) * self.tolerance) + tol += int(tol == 0) + indexs = dist.argsort()[:tol] + # choice = dist.argmin() # closet value + choice = random.choice(indexs) + return cluster[tuple(nodes[choice])] def buildMatrix(self): @@ -141,12 +128,6 @@ class Mosiac(): Build the image matrix. """ then = time.time() - # for row in range(self.smallImage.size[1]): - # new_row = [] - # for col in range(self.smallImage.size[0]): - # image = self.nearestImage(self.smallImage.getpixel((col, row))) - # new_row.append(image) - # self.imageMatrix.append(new_row) pixels = list(self.smallImage.getdata()) for pixel in pixels: image = self.nearestImage(pixel)