#!/usr/bin/env python3
# Copyright 2014 James P Goodwin slides tool
# This is a tool to automatically rotate and resize collections of images
import sys
import os
import re
from optparse import OptionParser
import traceback
from PIL import Image
from PIL.ExifTags import TAGS
import subprocess
from multiprocessing import Pool
from slides_sound.slides_util import cmpfile,run,saferemove

def process_frame( img_name, out_frame, width, height, verbose = False ):
    try:
        if verbose:
            print("Processing",img_name)
        img = Image.open(img_name,"r")
        exif = img._getexif()
        img_orientation = 0
        if exif:
            for (k,v) in list(exif.items()):
                if k in TAGS and TAGS[k] == "Orientation":
                    img_orientation = v
                    break

        if img_orientation == 6:
            if options.verbose:
                print("Rotating Image")
            img = img.rotate( -90, Image.BICUBIC, True )

        if verbose:
            print("Resizing to", width, height)

        fw = float(options.width)
        fh = float(options.height)
        iw = img.size[0]
        ih = img.size[1]
        sw = fh / ih
        iw = int(iw * sw)
        ih = int(ih * sw)
        img = img.resize( (iw, ih), Image.BICUBIC)
        nimg = Image.new( img.mode, (int(options.width),int(options.height)), "black")
        ox = (nimg.size[0] - img.size[0]) // 2
        oy = (nimg.size[1] - img.size[1]) // 2
        nimg.paste(img, (ox,oy))
        img = nimg

        if options.verbose:
            print("Saving Frame:",out_frame)
        img.save(out_frame)
    except:
        tb = traceback.format_exc()
        print(tb, file=sys.stderr)
        exit(1)

def main(options, args):
    """ The main driver for the slides utility """
    slides = []
    if options.file:
        for ln in open(options.file,"r"):
            slides.append(ln.strip())
    else:
        slides = sorted(args,key=lambda x: cmpfile(x))

    p = Pool(10)
    frame_idx = 0
    for img_name in slides:
        while True:
            out_name = "%s/frame%04d.jpg"%(options.path,frame_idx)
            if os.path.exists( out_name ):
                frame_idx += 1
            else:
                break
        p.apply_async(process_frame,( img_name, out_name, options.width, options.height, options.verbose ))
        frame_idx += 1
    p.close()
    p.join()


    if options.verbose:
        print("Done")
    return 0

if __name__ == '__main__':
    parser = OptionParser(usage="usage: %prog [options] [path with wildcard to the images]", description="A tool to resize all photos to a common size and rotate and pad any camera rotated images")
    parser.add_option("-f","--file", dest="file", default="", help="Read image file paths from a file one full path per line")
    parser.add_option("-v","--verbose", dest="verbose", action="store_true", default=False, help="Log all activity to console")
    parser.add_option("-x","--width", dest="width", default=320, type="int", help="Width of slide show (default 320)")
    parser.add_option("-y","--height", dest="height", default=240, type="int", help="Height of slide show (default 240)")
    parser.add_option("-p","--path", dest="path", default=".", help="Output path for transformed images")

    (options,args) = parser.parse_args()

    try:
        ret = main(options,args)
    except:
        tb = traceback.format_exc()
        print(tb, file=sys.stderr)
        exit(1)

    exit(ret)
