"""
photomosaic.py

Creates a photomosaic given a target image and a folder of input images

Author: Mahesh Venkitachalam
Libro: PYTHON PLAYGROUND
"""

import sys, os, random, argparse
from PIL import Image

"""
Python Imaging Library (PIL).
"""
import imghdr
import numpy as np

def getAverageRGB(image):
  """
  Given PIL Image, return average value of color as (r, g, b)
  """
  if image.mode != 'RGB':
    image = image.convert('RGB')

  # get image as numpy array
  im = np.array(image) 
  w,h,d = im.shape
  
  
  return tuple(np.average(im.reshape(w*h, d), axis=0))

def splitImage(image, size):
	#size es el grid_size
  """
  Given Image and dims (rows, cols) returns an m*n list of Images 
  """
  W, H = image.size[0], image.size[1]
  m, n = size
  w, h = int(W/n), int(H/m)
  print("w y h valen  ", w, h)
  # image list
  imgs = []
  # generate list of dimensions
  for j in range(m):
    for i in range(n):
      # append cropped image
      imgs.append(image.crop((i*w, j*h, (i+1)*w, (j+1)*h)))
  return imgs

def getImages(imageDir):
  """
  given a directory of images, return a list of Images
  """
  files = os.listdir(imageDir)
  images = []
  for file in files:
    filePath = os.path.abspath(os.path.join(imageDir, file))
    try:
      # explicit load so we don't run into resource crunch
      fp = open(filePath, "rb")
      im = Image.open(fp)
      images.append(im)
      # force loading image data from file
      im.load() 
      # close the file
      fp.close() 
    except:
      # skip
      print("Invalid image: %s" % (filePath,))
  return images

def getImageFilenames(imageDir):
  """
  given a directory of images, return a list of Image file names
  """
  files = os.listdir(imageDir)
  filenames = []
  for file in files:
    filePath = os.path.abspath(os.path.join(imageDir, file))
    try:
      imgType = imghdr.what(filePath) 
      if imgType:
        filenames.append(filePath)
    except:
      # skip
      print("Invalid image: %s" % (filePath,))
  return filenames

def getBestMatchIndex(input_avg, avgs):
  """
  return index of best Image match based on RGB value distance
  """

  # input image average
  avg = input_avg
  
  # get the closest RGB value to input, based on x/y/z distance
  index = 0
  min_index = 0
  #valor superior (sin limite) para utilizarse en la comparacion
  min_dist = float("inf")
  for val in avgs:
    dist = ((val[0] - avg[0])*(val[0] - avg[0]) +
            (val[1] - avg[1])*(val[1] - avg[1]) +
            (val[2] - avg[2])*(val[2] - avg[2]))
    if dist < min_dist:
      min_dist = dist
      min_index = index
    index += 1

  return min_index


def createImageGrid(images, dims):
  """
  Given a list of images and a grid size (m, n), create 
  a grid of images. 
  """
  m, n = dims
  print(m,n,len(images))
  # sanity check
  assert m*n == len(images)

  # get max height and width of images
  # ie, not assuming they are all equal
  width = max([img.size[0] for img in images])
  height = max([img.size[1] for img in images])

  # create output image
  grid_img = Image.new('RGB', (n*width, m*height))
  
  # paste images
  for index in range(len(images)):
    row = int(index/n)
    col = index - n*row
    grid_img.paste(images[index], (col*width, row*height))
    
  return grid_img


def createPhotomosaic(target_image, input_images, grid_size,
                      reuse_images=True):
  """
  Creates photomosaic given target and input images.
  """

  print('splitting input image...')
  # split target image 
  target_images = splitImage(target_image, grid_size)

  print('finding image matches...')
  # for each target image, pick one from input
  output_images = []
  # for user feedback
  count = 0
  batch_size = int(len(target_images)/10)

  # calculate input image averages
  avgs = []
  for img in input_images:
    avgs.append(getAverageRGB(img))
  
  #calcular promedio en target_image
  for img in target_images:
    # target sub-image average
    avg = getAverageRGB(img)
    # find match index entre avg (input_image) y avgs (target_image)
    match_index = getBestMatchIndex(avg, avgs)
    output_images.append(input_images[match_index])
    # user feedback
    if count > 0 and batch_size > 10 and count % batch_size == 0:
      print('processed %d of %d...' %(count, len(target_images)))
    count += 1
    # remove selected image from input if flag set
    if not reuse_images:
      output_images.remove(input_images[match_index])

  print('creating mosaic...')
  # draw mosaic to image
  mosaic_image = createImageGrid(output_images, grid_size)

  # return mosaic
  return mosaic_image


  ###### INPUTS ######


target_image = Image.open("Carmen3.png")
target_image.show()
	  
# input images
print('reading input folder...')

input_folder = "images"
input_images = getImages(input_folder)


	# check if any valid input images found  
if input_images == []:
	print('No input images found in %s. Exiting.' % (input_folder, ))
	exit()

	# size of grid, first argument vertical, second horizontal
grid_size = (int(20), int(20))
	
	# output
output_filename = 'mosaic.png'
  
	# re-use any image in input
reuse_images = True

	# resize the input to fit original image size
resize_input = True

	##### END INPUTS #####

print('starting photomosaic creation...')
  
	# if images can't be reused, ensure m*n <= num_of_images 
if not reuse_images:
	if grid_size[0]*grid_size[1] > len(input_images):
		print('number of images is less than the grid size')
		exit()
  
	# resizing input
if resize_input:
	print('resizing images...')
		# for given grid size, compute max dims w,h of tiles
	dims = (int(target_image.size[0]/grid_size[1]), 
			int(target_image.size[1]/grid_size[0])) 
	print("max tile dims: %s" % (dims,))
		# resize
	for img in input_images:
		img.thumbnail(dims)

	# create photomosaic
mosaic_image = createPhotomosaic(target_image, input_images, grid_size,
                                   reuse_images)
	# write out mosaic
mosaic_image.save(output_filename, 'PNG')

print("saved output to %s" % (output_filename,))
print('done.')
os.system("ristretto %s" % (output_filename))

	
