Ejemplo n.º 1
0
def mergeWith(a, b):
    if a is None:
        return b
    tot = float(a.count + b.count)
    r = (a.count / tot) * a.rgb_aves[0] + (b.count / tot) * b.rgb_aves[0]
    g = (a.count / tot) * a.rgb_aves[1] + (b.count / tot) * b.rgb_aves[1]
    b = (a.count / tot) * a.rgb_aves[2] + (b.count / tot) * b.rgb_aves[2]
    return Bucket(r, g, b, tot)
Ejemplo n.º 2
0
def bucketize(r, g, b, buckets):
    nb = Bucket(r, g, b)
    matches = filter(cutoff, [(nb.col.delta_e(bucket.col), bucket)
                              for bucket in buckets])
    numMatches = len(matches)
    if numMatches == 0:
        buckets.append(nb)
    elif numMatches == 1:
        ind = buckets.index(matches[0][1])
        buckets[ind].count += 1
    else:
        for (diff, bucket) in matches:
            buckets.remove(bucket)
        b = reduce(mergeWith, [bucket for (diff, bucket) in matches], nb)
        buckets.append(b)
Ejemplo n.º 3
0
def getInverseWeightedAverage(x, y, neighbors):
	totalDist = 0
	buckets = []
	for (n_x, n_y, (r,g,b)) in neighbors:
		xDif = x - n_x
		yDif = y - n_y
		dist = math.sqrt(xDif*xDif + yDif*yDif)
		totalDist += dist
		buckets.append( (Bucket(r,g,b), dist) )

	for (bucket, dist) in buckets:
		if dist == 0:
			bucket.count = 1
		else:
			bucket.count = (totalDist - dist) / dist

	return reduce(mergeWith, [bucket for (bucket, dist) in buckets])
Ejemplo n.º 4
0
def createMosaic(db, photo_filename):
	im = Image.open(photo_filename)
	pix = im.load()
	size = im.size
	print size
	
	labels = [[None for x in range(size[1])] for y in range(size[0])]
	d = collections.defaultdict(int)
	
	for x in range(size[0]):
		if x % 25 == 0:
			print x / float(size[0])
		for y in range(size[1]):
			(r,g,b) = im.getpixel((x,y))
			pix = [ Bucket(r,g,b) ]
			bestSample = selectBestImage(db, pix)
			d[bestSample] += 1
			labels[x][y] = bestSample
	
	print "Building photo"
	thumbs = createThumbnails(d)
	result = generateImage(labels, thumbs, size)
	return result