"""
Code for cropping of Soliton images to prepare them for edge detection.

Stephen Morris, June 2018

"""

from skimage.io import imread, imsave

from os import listdir, mkdir

path =   # path to the data folder containing the images
root_folder_name =    # name of the data folder

root_folder = path + '/' + root_folder_name   

crop_dir = path + '/' + root_folder_name + '_crop'
mkdir(crop_dir)  # new folder to hold the cropped images

# crop to these dimensions: you can set these by examining an image with Image J first

left  = 76      # pixels off the top, etc
right = 64      # it is best for the edge detection to crop as much off the top and bottom
top   = 500     # as possible, cutting just below the background interface from the bottom
bot   = 780     # and just above the top of the biggest wave off the top.
                # The ends should not show anything of the ends of the tank

file_list = listdir(root_folder)  
file_list = sorted([name for name in file_list if (name.endswith('.tif') and not name.startswith('.'))])  

i = 0   # new index for frame numbers

for name in file_list: 
    image = imread(root_folder+'/'+name) 
    image_cropped = image[ top:image.shape[0]-bot, left:image.shape[1]-right ] 
    imsave(crop_dir+'/'+name[:-9]+'_crop.'+('%.4d' % i)+'.tif', image_cropped)  
    # keeps the full name minus old frame number and .tif, adds the new frame number and stores as .tif
    # this name format should sort properly with sort(listdir) 
    i = i+1
