import os import pandas as pd from pathlib import Path from time import strptime code_dir = str(Path(os.getcwd()).parent) sites_csv_path = os.path.join(code_dir, "coastsnap_sites.csv") coastsnap_sites_csv = pd.read_csv(sites_csv_path) images_parent_dir = coastsnap_sites_csv.parent_directory[0] images_dir = os.path.join(images_parent_dir, "Images") stats_csv = pd.DataFrame(columns = ['site','# processed', '# photoshop', '# registered', 'stability', 'most recently deleted', 'latest_processed', 'latest_registered'] ) print("Retrieving snapshot for:") for site in os.listdir(images_dir): # Loop through SITES i=0 latest_processed_found = False print(site) to_append = [site, 0, 0, 0, 0, 'None deleted',None,None] processed = False photoshop = False registered = False latest_deleted_image_found = False latest_registered_image_found = False site_path = os.path.join(images_dir, site) processed_path = os.path.join(site_path,'Processed') photoshop_path = os.path.join(site_path,'Photoshop') registered_path = os.path.join(site_path, 'Registered') try: # Check if site contains 'Processed' directory processed_years_list = os.listdir(processed_path) processed_years_list = [x for x in processed_years_list if len(x) == 4] # remove files that aren't years processed_years_list.reverse() processed = True except: continue try: # Check if site contains 'Processed' directory photoshop_years_list = os.listdir(photoshop_path) photoshop_years_list = [x for x in photoshop_years_list if len(x) == 4] # remove files that aren't years photoshop_years_list.reverse() photoshop = True except: continue try: # Check if site contains 'Processed' directory registered_years_list = os.listdir(registered_path) registered_years_list = [x for x in registered_years_list if len(x) == 4] # remove files that aren't years registered_years_list.reverse() registered = True except: continue if processed: i=0 for year in processed_years_list: # Loop through YEARS processed_year_path = os.path.join(processed_path, year) processed_image_list = os.listdir(processed_year_path) processed_image_list.reverse() for image_filename in processed_image_list: # Loop through IMAGES i += 1 to_append[1] = i if photoshop: i=0 for year in photoshop_years_list: # Loop through YEARS year_path = os.path.join(photoshop_path, year) image_list = os.listdir(year_path) image_list.reverse() # Add latest processed image date try: if os.path.isfile(year_path + '/' + image_list[0]) and latest_processed_found == False: latest_processed_found = True print(image_list[0]) filename_list1 = image_list[0].split(".") date = filename_list1[3].split("_") image_date1 = date[0] + '-' + '{:02d}'.format(strptime(filename_list1[2],'%b').tm_mon) +'-'+ filename_list1[5] to_append[6] = image_date1 except: pass for image_filename in image_list: # Loop through IMAGES # Grab the latest image date in Processed year_path = year_path.replace('Photoshop', 'Registered') image_filename_no_suffix = image_filename.replace("_tagged",""); registered_image_path = year_path + '/' + image_filename_no_suffix[:-4] + '_registered.jpg' # Finding the Latest Deleted Image Logic: # Iterate through 'Images/Processed' if os.path.isfile(registered_image_path): # Find the latest registered image. # Add latest registered image date try: if latest_registered_image_found == False: filename_list2 = image_filename.split(".") date = filename_list2[3].split("_") image_date2 = date[0] + '-' + '{:02d}'.format(strptime(filename_list2[2],'%b').tm_mon) +'-'+ filename_list2[5] to_append[7] = image_date2 except: pass latest_registered_image_found = True # This is so the latest deleted image # isn't just the most recent image if (latest_registered_image_found and # Check if latest registered image has been found not latest_deleted_image_found and # Check if latest deleted image has already been found not os.path.isfile(registered_image_path) and # Check if photoshop registered image is also in 'Images/Registered' image_filename.endswith('.jpg')): # Sanity check: Make sure the file is an image latest_deleted_image_found = True filename_list = image_filename.split(".") date = filename_list[3].split("_") image_date = date[0] + '-' + '{:02d}'.format(strptime(filename_list[2],'%b').tm_mon) +'-'+ filename_list[5] to_append[5] = image_date i += 1 to_append[2] = i if registered: i=0 for year in registered_years_list: # Loop through YEARS registered_year_path = os.path.join(registered_path, year) registered_image_list = os.listdir(registered_year_path) registered_image_list.reverse() for image_filename in registered_image_list: # Loop through IMAGES i += 1 to_append[3] = i stats_csv_length = len(stats_csv) stats_csv.loc[stats_csv_length] = to_append # Add site stability data (# registered / # processed) for i, row in stats_csv.iterrows(): if stats_csv.at[i, '# photoshop'] != 0: stability = "{0:.0%}".format(stats_csv.at[i, '# registered'] / stats_csv.at[i, '# photoshop']) else: stability = None # or whatever value makes sense in your context stats_csv.at[i,'stability'] = stability stats_csv.set_index('site', inplace = True) output_file_path = os.path.join(code_dir, 'images_snapshot.csv') stats_csv.to_csv(output_file_path)