// Replace_Images macro by Christophe Leterrier // This macro batch replaces a single image in a multichannel stack (identified by a string ID, at given poisiton in the stack) using replacement single images (identified by another string ID) in the same folder. // Use case is when the transmission image is missed in a multichannel acquisition (ROI) becauuse of lamp shutting on too slowly, so another transmission image is separately acquired (ROIt) // v1.0 23/02/2020 macro "Replace_Images" { //*************** Initialization *************** // Save Settings saveSettings(); // Default values for the Options Panel SO_DEF = "_ROI"; REP_DEF = "_ROIt"; POS_DEF = 5; SAVE_DEF="In a folder next to the source folder"; // Initialize choices variables // Old options: "'Extracted' in the 'Data Edit' folder (relative)", "'Extracted' in the 'Data Edit' folder (absolute)", "Same folder in the 'Data Edit' folder" SAVE_ARRAY = newArray("In the source folder", "In a subfolder of the source folder", "In a folder next to the source folder", "In a folder with custom location"); //*************** Dialog 1 : get the input images folder path *************** INPUT_DIR = getDirectory("Select a source folder with raw images"); print("\n\n\n*** Replace_Images Log ***"); print(""); print("INPUT_DIR: " + INPUT_DIR); //*************** Dialog 2 : options *************** // Creation of the dialog box Dialog.create("Replace_Images Options"); Dialog.addString("Source multi-channel ID string", SO_DEF); Dialog.addString("Replacement image ID string", REP_DEF); Dialog.addNumber("Position of channel to replace", POS_DEF, 0, 5, ""); Dialog.addChoice("Save Images", SAVE_ARRAY, SAVE_DEF); Dialog.show(); // Feeding variables from dialog choices SO = Dialog.getString(); REP = Dialog.getString(); POS = Dialog.getNumber(); SAVE_TYPE = Dialog.getChoice(); // setBatchMode(true); //*************** Prepare Processing (get names, open images, make output folder) *************** // Get all file names ALL_NAMES = getFileList(INPUT_DIR); Array.sort(ALL_NAMES); N_LENGTH = ALL_NAMES.length; ALL_EXT = newArray(N_LENGTH); // Create extensions array for (i = 0; i < N_LENGTH; i++) { // print(ALL_NAMES[i]); ALL_NAMES_PARTS = getFileExtension(ALL_NAMES[i]); ALL_EXT[i] = ALL_NAMES_PARTS[1]; } // Create the output folder OUTPUT_DIR = INPUT_DIR; FOLDER_NAME = "rep"; if (SAVE_TYPE == "In the source folder") { OUTPUT_DIR = INPUT_DIR; } if (SAVE_TYPE == "In a subfolder of the source folder") { OUTPUT_DIR = INPUT_DIR + FOLDER_NAME + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } } if (SAVE_TYPE == "In a folder next to the source folder") { OUTPUT_DIR = File.getParent(INPUT_DIR); OUTPUT_NAME = File.getName(INPUT_DIR); OUTPUT_SHORTA = split(OUTPUT_NAME, " "); OUTPUT_SHORT = OUTPUT_SHORTA[0]; OUTPUT_DIR = OUTPUT_DIR + File.separator + OUTPUT_SHORT + " " + FOLDER_NAME + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } } if (SAVE_TYPE == "In a folder with custom location") { OUTPUT_DIR = getDirectory("Choose the custom location for the 'Extracted' folder"); OUTPUT_DIR = OUTPUT_DIR + File.separator + FOLDER_NAME + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } } OUTPUT_PARENT_DIR = File.getParent(OUTPUT_DIR); print("OUTPUT_DIR: " + OUTPUT_DIR); // print("OUTPUT_PARENT_DIR: " + OUTPUT_PARENT_DIR); //*************** Processing *************** // Loop on all .tif extensions for (n=0; n 0 && lastIndexOf(FILE_SHORTNAME, REP) < 0) { print(""); print("INPUT_PATH:", FILE_PATH); open(FILE_PATH); SO_ID = getImageID(); REP_SHORTNAME = replace(FILE_SHORTNAME, SO, REP); REP_NAME = REP_SHORTNAME + FILE_EXT; REP_PATH = FILE_DIR + File.separator + REP_NAME; if (File.exists(REP_PATH) == 1) { open(REP_PATH); REP_ID = getImageID(); run("Select All"); run("Copy"); selectImage(SO_ID); Stack.setChannel(POS); run("Paste"); // Close replacement image selectImage(REP_ID); close(); }// end of IF loop on REP file exists // Create output file path and save the output image OUTPUT_PATH = OUTPUT_DIR + FILE_NAME; selectImage(SO_ID); save(OUTPUT_PATH); print("OUTPUT_PATH: "+OUTPUT_PATH); // Close input stack close(); }// end of IF loop on SO containing name }// end of IF loop on tif extensions }// end of FOR loop on all files // setBatchMode("exit and display"); print(""); print("*** Replace_Images end ***"); showStatus("Replace Images finished"); } //*************** Functions *************** function getFileExtension(Name) { nameparts = split(Name, "."); shortname = nameparts[0]; if (nameparts.length > 2) { for (k = 1; k < nameparts.length - 1; k++) { shortname += "." + nameparts[k]; } } extname = "." + nameparts[nameparts.length - 1]; namearray = newArray(shortname, extname); return namearray; }