from os import listdir
from PIL import Image, ExifTags

files = listdir("people")

# Just to be safe:
if ".DS_Store" in files:
    files.remove(".DS_Store")

# This list will contain TUPLES of the form (DATE_TIME,IMAGE)
# Where DATE_TIME is the date & time of the image stored in IMAGE
sorted_images = []

# Loop over all files in the directory:
for f in files:
    img = Image.open( "people/" + f )

    # Get the Exif property corresponding to date / time, save that in date_time
    exif_dictionary = img.getexif()
    if 36867 in exif_dictionary:
        date_time = exif_dictionary[36867]

        # Loop over the sorted_images list and determine where
        # to insert the current image based on its date_time value
        i = 0
        while i < len(sorted_images) and date_time > sorted_images[i][0]:
            i = i + 1
        sorted_images.insert(i, (date_time,img) )

# Now create a new blank image that is
# 200 pixels tall and as wide as the length of the sorted_images list times 200
output_image = Image.new("RGB", (200*len(sorted_images),200), (255,255,255) )
x = 0
# Loop over sorted_images, which will iterate over each image in
# the order determined above
for img in sorted_images:
    # Resize each into a 200x200 box
    img[1].thumbnail((200,200))
    # And paste it into the new blank image at x,0
    output_image.paste(img[1],(x,0))
    # Increase x by 200 for the location of the next image
    x = x + 200

# Save and display the new image
output_image.save("new.jpg")
output_image.show()
