###############################################################################
#
# (c) Emmanuel Christophe emmanuel.christophe@gmail.com
#                       http://www.melaneum.com  
#
# This script is released under the GPL v2 license
#
###############################################################################

from kmlGen import *
import os,sys, fnmatch, pyexiv2, Image

#Function to retrieve data from exif tags
def getExifData(filename):
  image = pyexiv2.Image(filename)
  image.readMetadata()
  lonRational=image['Exif.GPSInfo.GPSLongitude']
  lon=eval('0.0+'+str(lonRational[0])+'+1/60.*'+str(lonRational[1])+'+1/3600.*'+str(lonRational[2]))
  latRational=image['Exif.GPSInfo.GPSLatitude']
  lat=eval('0.0+'+str(latRational[0])+'+1/60.*'+str(latRational[1])+'+1/3600.*'+str(latRational[2]))
  width=image['Exif.Photo.PixelXDimension']
  height=image['Exif.Photo.PixelYDimension']
  altitude=eval('1.0*'+str(image['Exif.GPSInfo.GPSAltitude']))
  datetime=image['Exif.Image.DateTime']
  orientation=image['Exif.Image.Orientation']
  return ("OK", lat, lon, width, height, datetime, altitude, orientation)

#Variable to set according to the desired output
folder="/where/the/photos/are"
url="http://www.mysite.sth/where/the/photos/and/thumbnail/will/be/"

myKml=KML(folder+"/doc-web",folder,url=url,gmaps=True)
myKml.writeInKml("\n<Folder>\n<name>Photos</name>")
for fileName in os.listdir ( folder ):
    if (fnmatch.fnmatch (fileName, '*.JPG') or fnmatch.fnmatch (fileName, '*.jpg')) and (not fnmatch.fnmatch (fileName, '_thb_*')) and (not fnmatch.fnmatch (fileName, 'w*')):
        print "\nFound fileName ",fileName," Processing now ..."
	result=getExifData(folder+'/'+fileName)
	print result
	
	#Create thumb and make a preview
	if fnmatch.fnmatch (fileName, '*.JPG') or fnmatch.fnmatch (fileName, '*.jpg'):
	    print "Create a rescaled version now!"
	    try:
	        print folder+'/'+fileName
		im=Image.open(folder+'/'+fileName)
		width=int(im.size[0])
		height=int(im.size[1])
		if width>height:
		    max=width
		else:
		    max=height
		zoom=float(800.0/max)
		im.thumbnail((int(width*zoom),int(height*zoom)))
		if result[7] == 6:
		  im=im.rotate(270)
		if result[7] == 8:
		  im=im.rotate(90)  
		im.save(folder+"/w"+fileName)
	    except:
		print "Warning: didn't create thumbnail, no JPG file ?"
	    print "Create a thumb now!"
	    try:
	        print folder+'/'+fileName
		im=Image.open(folder+'/'+fileName)
		width=int(im.size[0])
		height=int(im.size[1])
		if width>height:
		    max=width
		else:
		    max=height
		zoom=float(160.0/max)
		im.thumbnail((int(width*zoom),int(height*zoom)))
		if result[7] == 6:
		  im=im.rotate(270)
		if result[7] == 8:
		  im=im.rotate(90)  
		im.save(folder+"/_thb_w"+fileName)
	    except:
		print "Warning: didn't create thumbnail, no JPG file ?"
		



        myKml.placemark4Gmaps(folder+"/"+fileName,lat=result[1],long=result[2],width=result[3],height=result[4],elevation=result[6])
myKml.writeInKml("</Folder>\n")
myKml.close()