import Image
import math

def entropy(img):

	# calculate the shannon entropy for an image

	histogram = img.histogram()
	histogram_length = sum(histogram)

	samples_probability = [float(h) / histogram_length for h in histogram]

	return -sum([p * math.log(p, 2) for p in samples_probability if p != 0])

def focalpoint(img):
	
	# calculate the shannon entropy for small slices of an image
	
	w = img.size[0]
	h = img.size[1]
	dims = [ w,h ]

	crop_size = 300
	slice_size = 100

	tiles = []
	max_ent = 0
	
	for y in range(0,h,slice_size):
		for x in range(0,w,slice_size):
			mx = min(x+slice_size, w)
			my = min(y+slice_size, h)
			
			buff = Image.new("RGB", [slice_size, slice_size], (255, 255, 255)) 
			tile = img.crop((x, y, mx, my))
			buff.paste(tile, (0, 0))
		
			ent = entropy(buff)
			
			data = {'entropy':ent, 'x':x, 'y':y}

			if ent > max_ent:
				max_ent = ent
				tiles = data

        if tiles['x'] > slice_size:
                tiles['x'] -= slice_size
        
        if tiles['y'] > slice_size:
                tiles['y'] -= slice_size

        if (tiles['x'] + crop_size) > w:
                tiles['x'] = w - crop_size

        if (tiles['y'] + crop_size) > h:
                tiles['y'] = h - crop_size

	return tiles

if __name__ == '__main__' :

	import sys
        import pprint

	path = sys.argv[1]
	im = Image.open(path)

	tiles = focalpoint(im)
	print pprint.pformat(tiles)

@@ -1224,16 +1224,63 @@
 p_size%0A%0A
+        tiles%5B'w'%5D = w%0A        tiles%5B'h'%5D = h%0A%0A
 %09return 

return h/w in focalpoint