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():
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user