sorting imagebank before splitting into clusters

This commit is contained in:
iou1name 2017-12-10 12:31:42 -05:00
parent 4064c76fb0
commit 940efa5f52

View File

@ -6,18 +6,18 @@ Does mosiacs.
import os import os
import time import time
import random import random
import colorsys
import numpy as np import numpy as np
from PIL import Image from PIL import Image
class Mosiac(): class Mosiac():
def __init__(self): def __init__(self):
self.images = []
self.means = []
self.imageBank = {} self.imageBank = {}
self.numClusters = 8 self.numClusters = 8
self.clusters = [] self.clusters = []
self.clusterMeans = [] self.clusterMeans = []
self.bigImageSize = None self.bigImageSize = None
self.bigImage = None self.bigImage = None
self.smallImage = None self.smallImage = None
@ -50,12 +50,15 @@ class Mosiac():
image = image.convert(image.palette.mode) image = image.convert(image.palette.mode)
if image.mode == "RGBA": if image.mode == "RGBA":
image = alpha_composite_with_color(image).convert("RGB") image = alpha_composite_with_color(image).convert("RGB")
if image.mode == "L":
image = image.convert("RGB")
# image = image.convert("RGBA") # image = image.convert("RGBA")
image = image.resize(self.tileSize, Image.ANTIALIAS) image = image.resize(self.tileSize, Image.ANTIALIAS)
mean = tuple(np.mean(image, axis=(0,1))) mean = tuple(np.mean(image, axis=(0,1)))
# img = Image.new("RGBA", image.size) # img = Image.new("RGBA", image.size)
# img.putdata(list(map(lambda pixel: (255,255,255,0) if pixel == (255,255,255,255) else pixel, image.getdata()))) # img.putdata(list(map(lambda pixel: (255,255,255,0) if pixel == \
# (255,255,255,255) else pixel, image.getdata())))
self.imageBank[mean] = image self.imageBank[mean] = image
print(f"initImageBank took: {time.time()-then} seconds.") print(f"initImageBank took: {time.time()-then} seconds.")
@ -66,15 +69,12 @@ class Mosiac():
Create a bank of test images. Create a bank of test images.
""" """
then = time.time() then = time.time()
self.images = []
self.means = []
for i in range(0, 257, 16): for i in range(0, 257, 16):
for j in range(0, 257, 16): for j in range(0, 257, 16):
for k in range(0, 257, 16): for k in range(0, 257, 16):
image = Image.new("RGB", self.tileSize, (i,j,k, 255)) image = Image.new("RGB", self.tileSize, (i,j,k, 255))
self.images.append(image)
mean = tuple(np.mean(image, axis=(0,1))) mean = tuple(np.mean(image, axis=(0,1)))
self.means.append(mean) self.imageBank[mean] = image
print(f"debugImageBank took: {time.time()-then} seconds.") print(f"debugImageBank took: {time.time()-then} seconds.")
@ -87,8 +87,15 @@ class Mosiac():
num = len(sort) // self.numClusters num = len(sort) // self.numClusters
for n in range(self.numClusters): for n in range(self.numClusters):
cluster = dict(0: pixel for pixel in sort[n*num:n*num+num]) cluster = {pixel: self.imageBank[pixel] for pixel in \
cluster sort[n*num:n*num+num]}
self.clusters.append(cluster)
self.clusters[-1].update({pixel: self.imageBank[pixel] for pixel in \
sort[-(len(sort) % num):]})
for cluster in self.clusters:
mean = tuple(np.mean(list(cluster.keys()), axis=(0)))
self.clusterMeans.append(mean)
""" """
indexs = list(range(len(self.images))) indexs = list(range(len(self.images)))
step = len(self.images) // self.numClusters step = len(self.images) // self.numClusters
@ -114,12 +121,19 @@ class Mosiac():
clusterMean = self.clusterMeans[dists.index(min(dists))] clusterMean = self.clusterMeans[dists.index(min(dists))]
cluster = self.clusters[self.clusterMeans.index(clusterMean)] cluster = self.clusters[self.clusterMeans.index(clusterMean)]
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 = [] dists = []
for n in cluster: for mean in cluster.keys():
dist = np.linalg.norm(np.array(pixel)-np.array(self.means[n])) dist = np.linalg.norm(np.array(pixel)-np.array(mean))
dists.append(dist) dists.append(dist)
choice = random.choice(sorted(dists)[:10]) """
return self.images[cluster[dists.index(choice)]] # choice = random.choice(sorted(dists)[:10])
# return cluster[dists.index(choice)]
def buildMatrix(self): def buildMatrix(self):
@ -158,7 +172,7 @@ class Mosiac():
def step (r,g,b, repetitions=1): def step (r,g,b, repetitions=1):
lum = math.sqrt( .241 * r + .691 * g + .068 * b ) lum = ( .241 * r + .691 * g + .068 * b )**0.5
h, s, v = colorsys.rgb_to_hsv(r,g,b) h, s, v = colorsys.rgb_to_hsv(r,g,b)
@ -228,8 +242,8 @@ if __name__ == "__main__":
mosiac = Mosiac() mosiac = Mosiac()
mosiac.openBigImage(os.path.join(args.root, "big.jpg")) mosiac.openBigImage(os.path.join(args.root, "big.jpg"))
# mosiac.initImageBank(**vars(args)) mosiac.initImageBank(**vars(args))
mosiac.debugImageBank() # mosiac.debugImageBank()
mosiac.initClusters() mosiac.initClusters()
# mosiac.buildMatrix() mosiac.buildMatrix()
# mosiac.buildMosiac(args.root) mosiac.buildMosiac(args.root)