nearestImage is better
This commit is contained in:
parent
940efa5f52
commit
0759ec4199
37
mosiac.py
37
mosiac.py
|
@ -13,8 +13,10 @@ from PIL import Image
|
||||||
|
|
||||||
class Mosiac():
|
class Mosiac():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.imageBank = {}
|
|
||||||
self.numClusters = 8
|
self.numClusters = 8
|
||||||
|
self.tolerance = 0.5
|
||||||
|
|
||||||
|
self.imageBank = {}
|
||||||
self.clusters = []
|
self.clusters = []
|
||||||
self.clusterMeans = []
|
self.clusterMeans = []
|
||||||
|
|
||||||
|
@ -96,16 +98,6 @@ class Mosiac():
|
||||||
for cluster in self.clusters:
|
for cluster in self.clusters:
|
||||||
mean = tuple(np.mean(list(cluster.keys()), axis=(0)))
|
mean = tuple(np.mean(list(cluster.keys()), axis=(0)))
|
||||||
self.clusterMeans.append(mean)
|
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.")
|
print(f"initClusters took: {time.time()-then} seconds.")
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,17 +115,12 @@ class Mosiac():
|
||||||
|
|
||||||
nodes = np.array(list(cluster.keys()))
|
nodes = np.array(list(cluster.keys()))
|
||||||
dist = np.sum((nodes - np.array(pixel))**2, axis=1)
|
dist = np.sum((nodes - np.array(pixel))**2, axis=1)
|
||||||
# return np.argmin(dist) # closet value
|
tol = int(len(dist) * self.tolerance)
|
||||||
dist.argsort()[:10]
|
tol += int(tol == 0)
|
||||||
return random.choice(list(dist))
|
indexs = dist.argsort()[:tol]
|
||||||
"""
|
# choice = dist.argmin() # closet value
|
||||||
dists = []
|
choice = random.choice(indexs)
|
||||||
for mean in cluster.keys():
|
return cluster[tuple(nodes[choice])]
|
||||||
dist = np.linalg.norm(np.array(pixel)-np.array(mean))
|
|
||||||
dists.append(dist)
|
|
||||||
"""
|
|
||||||
# choice = random.choice(sorted(dists)[:10])
|
|
||||||
# return cluster[dists.index(choice)]
|
|
||||||
|
|
||||||
|
|
||||||
def buildMatrix(self):
|
def buildMatrix(self):
|
||||||
|
@ -141,12 +128,6 @@ class Mosiac():
|
||||||
Build the image matrix.
|
Build the image matrix.
|
||||||
"""
|
"""
|
||||||
then = time.time()
|
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())
|
pixels = list(self.smallImage.getdata())
|
||||||
for pixel in pixels:
|
for pixel in pixels:
|
||||||
image = self.nearestImage(pixel)
|
image = self.nearestImage(pixel)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user