# -*- coding: utf-8 -*-
"""
Created on Wed Aug 17 10:26:55 2016

@author: davidc
"""

#!/usr/bin/python
import numpy as np
import pylab   #image showing, an apendix to matplotlib
from PIL import Image, ImageChops
import pandas as pd  #data package like read csv
import os
#import matplotlib.pyplot as plt  #image showing
#   import exifread # https://pypi.python.org/pypi/ExifRead
#import pytesseract # https://pypi.python.org/pypi/pytesseract
                   # https://github.com/tesseract-ocr/tesseract/wiki

'''
These pictures are duplicates
#1343    train/21556793.jpeg
#1594    train/19990265.jpeg
#3410    train/19028923.jpeg
'''

#set working directory
os.chdir('/Users/davidc/Desktop/Research_Tools/2016_IPSW_500px')

def Photo_id_2_index(photo_id,train):
    return train['photo_id'][train['photo_id']==photo_id].index[0]

def ImagToJpeg(index,train):
    '''
    Function ImagToJpeg opens the jpeg:
    INPUTS:
    index: which picture do you want? 0, 1, 2, ...., 3677
    '''
    path = 'dataset/'+ train['image_path'][index]
    img_jpg = Image.open(open(path,'rb'))
    return img_jpg
    
def JpegToArray(img_jpg):
    '''
    Function JpegToArray converts the jpeg to an array:
    INPUTS:
    index: file in a jpg image type
    '''
    img_arry = np.asarray(img_jpg, dtype='float64')  # Converts Jpeg to Array
    return img_arry
    
def ViewArray(img_arry):
    '''
    Function ViewImg outputs the image:
    INPUTS:
    img: array returned from JpegToArray
    '''
    pylab.imshow(img_arry)
    
def rgb2grey(rbg):
    '''
    Function rgb2grey converts an array image to greyscale
    INPUTS:
    index: img_array
    '''
    return np.dot(rbg[...,:3], [0.299, 0.587, 0.114])
    
def ViewImagIndex(index,train):
    '''
    Function ViewImagIndex give the index 
    INPUTS:
    index:  
    '''
    View_img_jpg = ImagToJpeg(index,train)
    pylab.imshow(View_img_jpg)

def Is_there_a_border(index,train):
    '''
    Function ViewImagIndex give the index 
    INPUTS:
    index:  
    '''
    im = ImagToJpeg(index,train)
    bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
    diff = ImageChops.difference(im, bg)
#    diff = ImageChops.add(diff, diff, 2.0, -50)
    diff = ImageChops.add(diff, diff, 2.0, -int(np.percentile(JpegToArray(diff),25)))
    bbox = diff.getbbox()
    LB = not (bbox[1] == 0)
    RB = not (bbox[3] == im.size[1])
    TB = not (bbox[0] == 0)
    BB = not (bbox[2] == im.size[1])
    borders = ( (LB and RB) ) # or (TB and BB) or (LB and TB) or (LB and BB) or (RB and TB) or (RB and BB)
    return borders

def Is_there_a_border_vec(index,train):
    '''
    Function Is_there_a_border_vec puts into a vector 
    INPUTS:
    index:  
    '''
    temp = np.zeros(index.size)
    for i in index:
        temp[i] = Is_there_a_border(i,train)
    return temp
#Is_there_a_border_vec = np.vectorize(Is_there_a_border, excluded=['train'])
                           
def CompBordList(Predicted_Border):
    '''
    Function CompBordList gives photo_id if label is true border 
    INPUTS:
    Predicted_Border: algorithm chosen pics with borders  
    '''
    BenMar3col = pd.read_csv('train_borders3.csv')
    BM_border = BenMar3col['photo_id'][BenMar3col['label']==2]
    BM_border = BM_border.values
    return BM_border            
            












