'''
Created on Jan 11, 2022

@author: nathanielc

python file located in the Image Compression project
folder under nathanielc user. This python file is for
exploring possible image compression techniques and is by 
no means a final product. Goal is to learn how to download 
images and extract important statistics from them.
'''

import numpy as np
from matplotlib import pyplot as plt
from itertools import product
import os
import sys
from PIL import Image


def file_extractor(dirname="/media/elphel/SSD3-4GB/lwir16-proc/captures/teasdale/scenes"):
    files = os.listdir(dirname)
    scenes = []
    for file in files:
        scenes.append(os.path.join(dirname, file))
    return scenes

def image_extractor(scenes):
    image_folder = []
    for scene in scenes:
        files = os.listdir(scene)
        for file in files:
            image_folder.append(os.path.join(scene, file))
    images = []
    for folder in image_folder:
        ims = os.listdir(folder)
        for im in ims:
            if im[-4:] == ".jp4" or im[-7:] == "_6.tiff":
                continue
            else:
                images.append(os.path.join(folder, im))
    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor

if __name__ == '__main__':

    """For boundary cases: Start by grabbing the shape of the images and saving those
    as variables. Then, if statements for if row == 0 or row == maximum and if
    col == 0 or col == maximum. Then grab corresponding open pixels. Then proceed to do
    an and statement that handles the corners"""
    tiff0 = []
    tiff1 = []
    tiff2 = []
    tiff3 = []
    
    files = os.listdir("images")
    scenes = []
    for file in files:
        scene = os.path.join("images", file)
        sc = os.listdir(scene)
        for s in sc:
            if s[-6] == '0':
                tiff0.append(os.path.join(scene, s))
            elif s[-6] == '1':
                tiff1.append(os.path.join(scene, s))
            elif s[-6] == '2':
                tiff2.append(os.path.join(scene, s))
            elif s[-6] == '3':
                tiff3.append(os.path.join(scene, s))
 
    diff = []
    for ii in range(len(tiff1)):
        image = Image.open(tiff1[ii])    #Open the image and read it as an Image object
        image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data
        ar1, ar2 = image.shape
       
        ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column, 
                                                                        #this is now a random pixel selection within the image
        
        surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels
        for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices
            if i == 0 and j == 0:                                       #Avoid the target pixel
                continue
            else:
                surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list
        diff.append(np.max(surrounding)-np.min(surrounding))
        
    

    
    
    
    
    """scene_names = file_extractor("images")
    images = image_extractor(scene_names)
    im = Image.open(images[0])
    imarray = np.array(im)
    randos = np.random.randint(0,len(images), 10)
    i, j = np.random.randint(0,imarray.shape[0]), np.random.randint(0,imarray.shape[1])
    image_array = []
    for r in randos:
        image_array.append(np.array(Image.open(images[r])))
    indices = [val[i][j] for val in image_array]"""

    