diff --git a/coastsnap/photoshop_registration_one_site.jsx b/coastsnap/photoshop_registration_one_site.jsx index 037d196..fd9bfc8 100644 --- a/coastsnap/photoshop_registration_one_site.jsx +++ b/coastsnap/photoshop_registration_one_site.jsx @@ -9,12 +9,12 @@ // batch_images_to_register = Batch images to be registered (no target or seed) // batch_images_all = Target, seed and batch images to be registered +var site_name = "cooya" // ADD SITE NAME HERE var batch_size = 15; var parent_dir = File($.fileName).parent.parent.fsName; var batch_download_csv = File(parent_dir + "/coastsnap_sites.csv") -var site_name = "cooya" // retreive site names from batch_download.csv var csv_data=[]; diff --git a/coastsnap/tag_registered.py b/coastsnap/tag_registered.py index 5307a6b..0b04175 100644 --- a/coastsnap/tag_registered.py +++ b/coastsnap/tag_registered.py @@ -15,11 +15,13 @@ import pandas as pd from loguru import logger from moviepy.editor import * +import os +os.chdir(Path(__file__).parent.resolve()) + from utils import divide_chunks, datenum_to_datetime, nearest, progressbar, RegisteredImage import time # progress bar from PIL import Image, ImageFont, ImageDraw -import os from time import strptime from dateutil import tz @@ -39,34 +41,33 @@ For each site in Images Parent Directory: ''' + #-----------------------------------------------------------------------------# # Update this file path for Image Tagging - Tide Data -# Example: parent_dir = '/Users/admin/OneDrive - UNSW/My files/CoastSnap/' +# Example: images_dir = '/Users/admin/OneDrive - UNSW/My files/CoastSnap/' -parent_dir_ACTUAL = r"C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap" +code_images_dir = str(Path(os.getcwd()).parent) +sites_csv_path = os.path.join(code_images_dir, "coastsnap_sites.csv") -#-----------------------------------------------------------------------------# -# RETRIEVE IMAGES PARENT DIRECTORY IN .csv# - -coastsnap_sites = pd.read_csv("C:/Users/z5079346/OneDrive - UNSW/Projects/Coastsnap_test/CoastSnap_Sites.csv") -parent_directory = coastsnap_sites.parent_directory[0] -print(parent_directory) +# RETRIEVE IMAGES PARENT DIRECTORY IN 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") #-----------------------------------------------------------------------------# -folder = r"C:\Users\z5079346\OneDrive - UNSW\Projects\Coastsnap_test\Images_Test\cathieillaroo\Photoshop\2021" -def get_site_tide_data(parent_dir, site): +def get_site_tide_data(images_parent_dir, site): # Retrieve tide data for the given site - db = openpyxl.load_workbook(parent_dir + "/Database/CoastSnapDB.xlsx") + db = openpyxl.load_workbook(images_parent_dir + "/Database/CoastSnapDB.xlsx") beach_data = db[site] tide_filename = beach_data["B24"].value if tide_filename == 'NO_TIDE.mat': return False - mat_path = parent_dir + '/Tide Data/' + tide_filename + mat_path = images_parent_dir + '/Tide Data/' + tide_filename mat = sio.loadmat(mat_path) tide_dict = mat['tide'] ts = tide_dict[0][0] # Format of tide/time Matlab data is gross @@ -99,12 +100,12 @@ def tag_image(image2tag): image.save(registered_path) -for site in os.listdir(parent_directory): # Loop through SITES +for site in os.listdir(images_dir): # Loop through SITES i=0 site_complete = False # A flag to stop tagging images when found tide_data = False font = None - photoshop_path = parent_directory +'/'+ site + '/Photoshop' + photoshop_path = images_dir +'/'+ site + '/Photoshop' try: # Check if site contains 'Processed' directory years_list = os.listdir(photoshop_path) years_list.reverse() @@ -138,7 +139,7 @@ for site in os.listdir(parent_directory): # Loop through SITES pathname = os.path.join(year_path, image_filename) image2tag = RegisteredImage(pathname, image_filename) # Create image object if i == 1: # Retrieve Tide Data once for each site - site_tide_data, ts = get_site_tide_data(parent_dir_ACTUAL, site) + site_tide_data, ts = get_site_tide_data(images_parent_dir, site) font = image2tag.get_font() @@ -148,172 +149,3 @@ for site in os.listdir(parent_directory): # Loop through SITES image2tag.create_tag() tag_image(image2tag) - - - - - - - - - -""" -Tags images based on file name. -- Requires font file in coastsnap/fonts directory -""" - -""" -def tag_images(folder): - photoshop = True - tide = False - - # Get image paths - img_paths = [x for x in Path(folder).glob("*.jpg")] - img_names = [str(x) for x in img_paths] - logger.info(f"Tagging {len(img_names)} images") - - # Initialise white text box - rect_height = 1 - image_width = 1 - - # Check whether the directory 'tagged' exists or not - path_name = img_names[0] - tagged_dir = str(Path(path_name).parent) + '/tagged' - isExist = os.path.exists(tagged_dir) - if not isExist: - # Create a new directory because it does not exist - os.makedirs(tagged_dir) - - # Tide Data - if tide: - # Retrieve the site name from the first image - filename = Path(img_names[0]).name - - filename_list = filename.split(".") - site_name = filename_list[6] - - # Retrieve tide data for the given site - db = openpyxl.load_workbook(parent_dir + "/Database/CoastSnapDB.xlsx") - beach_data = db[site_name] - tide_filename = beach_data["B24"].value - mat_path = parent_dir + '/Tide Data/' + tide_filename - mat = sio.loadmat(mat_path) - tide_dict = mat['tide'] - ts = tide_dict[0][0] # Format of tide/time Matlab data is gross - tt = list(ts[0]) # ts[0] = tides, ts[1] = times, ts[2] = - - print("Loading tide data... (this may take half a minute)") - tide_times = [datenum_to_datetime(i) for i in tt] # THIS STEP TAKES A LONG TIME - - - fontsize = 1 - for index, img in enumerate(sorted(img_names)): - print("Image " + str(index+1)) - filename = str(Path(img).name) - image = Image.open(img) - draw = ImageDraw.Draw(image) - - - # Retrieve tag information from file name - filename_list = filename.split(".") - posix_time = filename_list[0] - date = filename_list[3].split("_") - hour = date[1] - minute = date[2] - second = date[3] - day = date[0] - month = '{:02d}'.format(strptime(filename_list[2],'%b').tm_mon) # Ensure 2-digit format - year = filename_list[5] - timezone = filename_list[4] - if 'snap' in filename_list: - contributor = filename_list[8] # Mitch filename format - else: - contributor = filename_list[6] # Leaman filename format - - # Retrieve tide data - if tide: - - # Account for daylight savings - # ASSUMPTION: All .mat tide files are in AEST. - if timezone == 'AEDT': - hour = str(int(hour) - 1) - date_string = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second - img_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") # Image date/time as a datetime object - - tide_date = nearest(tide_times, img_datetime) - mat_index = tide_times.index(tide_date) # Retrieve the index of the .mat tide/time - mat_tide = round(ts[1][mat_index][0], 2) # Associated tide - - print('Image date/time: ' + date_string) - print('Tide record: ' + str(tide_date)) - - - # Image tag - if tide: - txt = ('Date:' + year + '/' + month + '/' + day + - ' Time:' + hour + ':' + minute + - ' Contributor:' + contributor + - ' Tide:' + str(mat_tide) + 'm AHD') - else: - txt = ('Date:' + year + '/' + month + '/' + day + - ' Time:' + hour + ':' + minute + - ' Contributor:' + contributor) - - - # Set the fontsize, such that the tag covers 50% the width of the first image - if index == 0: - img_fraction = 0.8 # Change this to change the font size - - # White Text Box - image_width, image_height = image.size - rect_height = image_height/20 - rect_width = image_width/1.5 - font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) - - while font.getsize(txt)[0] < img_fraction*image.size[0]: - # iterate until the text size is just larger than the criteria - fontsize += 1 - font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) - font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) - - # Create white text box - draw.rectangle((0, 0, image_width, rect_height), fill='white') - - # Tag image with text - draw.text((20, rect_height/4),txt, font = font, fill=(0, 0, 0)) - - new_name = filename + '_registered.jpg' - print(new_name + '\n') - new_path = tagged_dir + "/" + new_name - image = image.convert('RGB') - image.save(new_path) - - logger.info(f"Tagged Images for " + + " Saved") - - -tag_images(folder) - -""" - - -# # Set Image Label Info Based on Target Image (PUT AT LINE 90) -# target_image_path = str(Path(path_name).parent.parent.parent) + r"\Target Image\Target.JPG" -# print(target_image_path) -# image = Image.open(target_image_path) -# img_fraction = 0.6 # Change this to change the font size -# fontsize = 1 - -# # White Text Box -# width, height = image.size -# rect_height = height/20 -# rect_width = width/1.5 -# font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) - -# while font.getsize(txt)[0] < img_fraction*image.size[0]: -# # iterate until the text size is just larger than the criteria -# fontsize += 1 -# font = ImageFont.truetype("fonts/Courier New Bold.ttf", fontsize) - -#"C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap\Images\cathieillaroo\Registered\2022" -#"C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap\Images\cathieillaroo\Target Image\Target.JPG" - diff --git a/coastsnap_sites.csv b/coastsnap_sites.csv index 7e5c8df..6305d47 100644 --- a/coastsnap_sites.csv +++ b/coastsnap_sites.csv @@ -1,5 +1,5 @@ site_name,root_id,limit,type,parent_directory -alex,487451,300,CoastSnap Station,C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap\Images +alex,487451,300,CoastSnap Station,C:\Users\z5079346\OneDrive - UNSW\My files\CoastSnap birubi,548070,300,CoastSnap Station, blacksmiths,269988,300,CoastSnap Station, broulee,269990,300,CoastSnap Station,