From c6138c5a192b4085147944cc8644dceb74cc2697 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Fri, 8 Jul 2022 18:24:55 +1000 Subject: [PATCH] Added more functionality for image tagging (tag reregistered, tag deleted registered, tag all and overwrite), AND added the '_tagged' prefix --- .../photoshop_registration_all_sites.jsx | 17 +- coastsnap/photoshop_registration_one_site.jsx | 342 ------------------ coastsnap/tag_registered.py | 176 +++++++-- coastsnap/utils.py | 7 +- 4 files changed, 167 insertions(+), 375 deletions(-) delete mode 100644 coastsnap/photoshop_registration_one_site.jsx diff --git a/coastsnap/photoshop_registration_all_sites.jsx b/coastsnap/photoshop_registration_all_sites.jsx index 161ed35..5a7005c 100644 --- a/coastsnap/photoshop_registration_all_sites.jsx +++ b/coastsnap/photoshop_registration_all_sites.jsx @@ -107,18 +107,19 @@ function batch_register_images(batchIndex, site_path, site_name, images_to_regis } -function imagesNotRegistered(site_images_all, photoshop_images) { +function imagesNotRegistered(processed_images, photoshop_images) { - var site_images = []; + var processed_images_to_register = []; // Extract images that haven't been registered - for (var i = 0; i < site_images_all.length; i ++) { // For each site image + for (var i = 0; i < processed_images.length; i ++) { // For each site image var isRegistered = false; for (var j = 0; j < photoshop_images.length; j++ ) { // Check if already exists in photoshop images - //var site_name_CHECK = site_images_all[i].name.slice(0,-4) + '_CHECK.jpg'; - var site_image = site_images_all[i].name.toString() + var processed_tagged = processed_images[i].name.slice(0,-4) + '_tagged.jpg'; + processed_tagged = processed_tagged.replace("%20", "_") + var site_image = processed_images[i].name.toString() site_image = site_image.replace("%20", "_") - if((site_image == photoshop_images[j].name.toString())) { + if((site_image == photoshop_images[j].name.toString()) || (processed_tagged == photoshop_images[j].name.toString())) { isRegistered = true; break; } @@ -128,10 +129,10 @@ function imagesNotRegistered(site_images_all, photoshop_images) { //var s = site2add.replace("%20", "_") // getFiles() reads in white space as "%20". So convert to "_" //alert(s) //var s = File(site_image); - site_images.push(site_images_all[i]); + processed_images_to_register.push(processed_images[i]); } } - return site_images + return processed_images_to_register } function stackFiles(sFiles){ diff --git a/coastsnap/photoshop_registration_one_site.jsx b/coastsnap/photoshop_registration_one_site.jsx deleted file mode 100644 index 84609d5..0000000 --- a/coastsnap/photoshop_registration_one_site.jsx +++ /dev/null @@ -1,342 +0,0 @@ -// SCRIPT LOGIC -// for site in sites: - // for year in years: - // register_images() - - -// VARIABLE DEFINITIONS -// images_to_register = All images to be registered for a given site and year (no target or seed) -// 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") - -// retreive site names from batch_download.csv -var csv_data=[]; -batch_download_csv.open('r'); -while(!batch_download_csv.eof){ - var InputLine = batch_download_csv.readln(); - if(InputLine.length > 3) csv_data.push(InputLine); -} -batch_download_csv.close(); -var site_names = csv_data.toString().split(",") - -// Retrieve images parent directory from CoastSnap_Sites.csv -var parent_folder_path = File(site_names[9]); - -var batch_images_to_register = []; // Used in exportLayersToPNG - -var site_path = parent_folder_path + "/Images/" + site_name; - -// Retrieve target and seed images for the site -var target_folder = new Folder(site_path + '/Target Image'); -var seed_folder = new Folder(site_path + '/Target Image' + '/Seed Images'); -var target_image = target_folder.getFiles("Target.jpg"); -var seed_images = seed_folder.getFiles("*.jpg"); - -// Retrieve processed image years for the site -var processed_folder = new Folder(site_path + '/Processed'); -var processed_subFolders = processed_folder.getFiles() - -// Loop through years -for (var j = 0; j < processed_subFolders.length; j++) { - - var year = processed_subFolders[j].name.toString(); - - var processed_folder = new Folder(site_path + '/Processed/' + year); - var photoshop_folder = new Folder(site_path + '/Photoshop/' + year); - var processed_images_all = processed_folder.getFiles("*.jpg"); - var photoshop_images = photoshop_folder.getFiles("*.jpg"); - - // Ignore images in processed that have already been registered - var images_to_register = imagesNotRegistered(processed_images_all, photoshop_images); - - // Register images in batches, to avoid reaching the image limit that causes photoshop to crash - for (var k = 0; k < images_to_register.length; k += batch_size) { - batch_register_images(k, site_path, site_name, images_to_register, target_image, seed_images); - } -} - - - -// This is the main function, responsible for calling photoshop functions -// in a sequential order to register all_images -function batch_register_images(batchIndex, site_path, site_name, images_to_register, target_image, seed_images) { - - var batch_images_all = []; - batch_images_to_register = []; - batch_images_all.push(target_image[0]) - - for (var i = 0; i < seed_images.length; i++ ) { - batch_images_all.push(seed_images[i]); - } - - for (var i = batchIndex*1; i < batchIndex + batch_size; i++) { - if(i < images_to_register.length) { - batch_images_all.push(images_to_register[i]); - batch_images_to_register.push(images_to_register[i].name); - - } - } - - stackFiles(batch_images_all); - resizeLayers(); - lockTarget(); - selectAllLayers(); - autoAlign(); - var target_size = cropToTarget(); - var target_width = target_size[0]; - var target_height = target_size[1]; - - savePhotoshopDocument(site_path, site_name); - exportLayersToPNG(target_width, target_height, batch_images_to_register, seed_images); // Won't overwrite images that already exist - - app.activeDocument.close(SaveOptions.DONOTSAVECHANGES) -} - - -function imagesNotRegistered(site_images_all, photoshop_images) { - - var site_images = []; - - // Extract images that haven't been registered - for (var i = 0; i < site_images_all.length; i ++) { // For each site image - var isRegistered = false; - for (var j = 0; j < photoshop_images.length; j++ ) { // Check if already exists in photoshop images - var site_name_CHECK = site_images_all[i].name.slice(0,-4) + '_CHECK.jpg'; - if((site_images_all[i].name.toString() == photoshop_images[j].name.toString()) || (site_name_CHECK.toString() == photoshop_images[j].name.toString())) { - isRegistered = true; - break; - } - } - if(!isRegistered) { // If it doesn't, register it - site_images.push(site_images_all[i]); - } - } - return site_images -} - -function stackFiles(sFiles){ - - var loadLayersFromScript = true; - var SCRIPTS_FOLDER = decodeURI(app.path + '/' + localize('$$$/ScriptingSupport/InstalledScripts=Presets/Scripts')); - $.evalFile( new File(SCRIPTS_FOLDER + '/Load Files into Stack.jsx')); - loadLayers.intoStack(sFiles, false); - -}; - -function resizeLayers() { - - var doc = app.activeDocument; - var targetImage = app.activeDocument.artLayers.getByName("Target.jpg") - - // Retrieve Target image dimensions - var target_bounds = targetImage.bounds; - var target_width = target_bounds[2] - target_bounds[0]; - var target_height = target_bounds[3] - target_bounds[1]; - - // Update Target Image dimensions - var new_width = 1280; - preferences.rulerUnits = Units.PIXELS; - var s = (new_width/target_width)*100; - targetImage.resize(s, s, AnchorPosition.TOPLEFT) - - // Retrieve new Target image dimensions - var target_bounds = targetImage.bounds; - var target_width = target_bounds[2] - target_bounds[0]; - var target_height = target_bounds[3] - target_bounds[1]; - - // Make layers similar dimensions to new Target - var layerName = doc.layers; - for (var ii = 0; ii < layerName.length; ii++) { //looping through all the layers - var current_layer = layerName[ii]; - preferences.rulerUnits = Units.PIXELS; - var b = current_layer.bounds; - var layer_width = b[2]-b[0]; - var layer_height = b[3]-b[1]; - if (layer_height >= layer_width) { - var s = (target_height/layer_height)*100; - } - else { - var s = (target_width/layer_width)*100; - } - current_layer.resize(s, s, AnchorPosition.TOPLEFT) - } -} - -function selectAllLayers() { - - var desc = new ActionDescriptor(); - var ref = new ActionReference(); - ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); - desc.putReference( charIDToTypeID('null'), ref ); - executeAction( stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO ); - -}; - - -function autoAlign() { - - var desc = new ActionDescriptor(); - var ref = new ActionReference(); - - ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); - desc.putReference( charIDToTypeID('null'), ref ); - desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('ADSt'), stringIDToTypeID('ADSContent') ); - desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('projection'), charIDToTypeID('Auto') ); - desc.putBoolean( stringIDToTypeID('vignette'), false ); - desc.putBoolean( stringIDToTypeID('radialDistort'), false ); - - executeAction( charIDToTypeID('Algn'), desc, DialogModes.NO ); - -}; - -function lockTarget() { - var layerRef = app.activeDocument.artLayers.getByName("Target.jpg") - layerRef.allLocked = true; - app.activeDocument.activeLayer.linkedLayers -} - - -// CROP -function cropToTarget() { - var layerRef = app.activeDocument.artLayers.getByName("Target.jpg") - app.activeDocument.crop(layerRef.bounds) - var theBounds = app.activeDocument.activeLayer.bounds; - var layerWidth = theBounds[2] - theBounds[0]; - var layerHeight = theBounds[3] - theBounds[1]; - return [layerWidth, layerHeight]; - -} - -// TO BE IMPLEMENTED -// function imageSize(layerRef) { - -// } - -// SAVE PHOTOSHOP DOCUMENT -function savePhotoshopDocument(site_path, site_name) { - var PSdocumentFile = new File(site_path + '/' + site_name + '.psd'); - app.activeDocument.saveAs(PSdocumentFile) -} - - - -// EXPORT LAYERS TO IMAGES - -// $.evalFile(File(app.path + '/Presets/Scripts/Export Layers To Files.jsx')); -function exportLayersToPNG(target_width, target_height, batch_images_to_register, seed_images){ - if(!documents.length) return; - var doc = activeDocument; - var oldPath = activeDocument.path; - - var outFolder = new Folder(oldPath + "/Photoshop/" + year); - if (!outFolder.exists) { - outFolder.create(); - } - - scanLayerSets(doc); - - function scanLayerSets(el) { - - // // find layer groups - // for(var a=0;a 0: + reregister = input("\n\n Do you want to tag 'all' " + str(count) + " images or just 'some' of them: ") + + print("\n") + tag = True + if reregister == 'all': + print("Tagging ALL reregistered images...") + elif reregister == 'some': + print(" Tagging SOME reregistered images. For each image:\n\n - to TAG, type the letter 'y' and hit enter \n - to SKIP, just hit enter\n") + reregister_all = False + else: + print(" User must type either 'all' or 'some' and then hit enter.") + exit() + if args.site: # Case 1: User specifies a site + loop_reregistered(args.site, tag, reregister_all) + else: # Case 2: No site specified. Tag all images + for site in os.listdir(images_dir): + loop_reregistered(site, tag, reregister_all) + +#-----------------------------------------------------------------------------# +# Case: NOT reregistering images + +else: + # Calling the main function with the two cases + if args.site: # Case 1: User specifies a site + main(args.site) + else: # Case 2: No site specified. Tag all images + for site in os.listdir(images_dir): + main(site) + + + + + + + + + + + + + + + + -if args.site: # Case 1: User specifies a site - main(args.site) -else: # Case 2: No site specified. Tag all images - for site in os.listdir(images_dir): - main(site) diff --git a/coastsnap/utils.py b/coastsnap/utils.py index 6ad9340..acc1916 100644 --- a/coastsnap/utils.py +++ b/coastsnap/utils.py @@ -54,7 +54,7 @@ def progressbar(it, prefix="", size=60, out=sys.stdout): # Python3.3+ def get_site_tide_data(images_parent_dir, site): - + print("Retrieving tide data") # Retrieve tide data for the given site db = openpyxl.load_workbook(images_parent_dir + "/Database/CoastSnapDB.xlsx") beach_data = db[site] @@ -62,7 +62,7 @@ def get_site_tide_data(images_parent_dir, site): if tide_filename == 'NO_TIDE.mat': return False - + tide_filename_pkl = tide_filename[:-4] + '.pkl' tides_path = images_parent_dir + '/Tide Data/Tide_Data_Python/' + tide_filename_pkl tides_df = pd.read_pickle(tides_path) @@ -71,9 +71,10 @@ def get_site_tide_data(images_parent_dir, site): class RegisteredImage(): - def __init__(self, pathname, filename): + def __init__(self, pathname, filename, registered_pathname): self.pathname = pathname self.filename = filename + self.registered_pathname = registered_pathname filename_list = filename.split(".") date = filename_list[3].split("_") if 'snap' in filename_list: