Based off an example batch Jython macro, I have created the below script, which works beautifully for my purposes. What is weird is that it hangs on macOS Catalina 10.15.1 when analyzing a folder on an external drive. It gets through a few images and then ImageJ silently quits. It works great on ALL images when the images are on the internal HD. I have tested with the same images in both locations.
Seems like it might be an issue with macOS and possible permissions?
import os, sys from ij.io import FileSaver from ij import IJ, ImagePlus, ImageStack, WindowManager from ij.plugin import RGBStackMerge from ij.process import ImageConverter sourceDir = "/Users/myname/Desktop/testimages" targetDir = "/Users/nyame/Desktop/output" #fails if targetdir is on an external drive, location of souceDir doesn't seem to matter # A function that takes an stack and adjust color and contrast and then makes RGB def normalizeContrast(imp): stack1 = imp.getImageStack() print imp.getTitle() print stack1.getSize() dapi_info = stack1.getProcessor(1) dapi_imp = IJ.createImage("DAPI", 284, 154,1,8) dapi_imp.setProcessor(dapi_info) IJ.run(dapi_imp, "Blue", "") fitc_info = stack1.getProcessor(2) fitc_imp = IJ.createImage("FITC", 284, 154,1,8) fitc_imp.setProcessor(fitc_info) IJ.run(fitc_imp, "Green", "") IJ.run(fitc_imp, "Enhance Contrast...", "saturated=0.3 process_all") final = RGBStackMerge.mergeChannels([dapi_imp,fitc_imp], False) ImageConverter(final).convertToRGB() copy_ip = final.getProcessor().duplicate() # Return as new image return ImagePlus(imp.getTitle(), copy_ip) # A function that takes a file path, attempts to load it as an image, # normalizes it, and saves it in a different directory def loadProcessAndSave(sourcepath, fn): try: imp = IJ.openImage(sourcepath) norm_imp = fn(imp) # invoke function 'fn', in this case 'normalizeContrast' targetpath = os.path.join(targetDir, os.path.basename(sourcepath)) if not targetpath.endswith(".png"): targetpath += ".png" FileSaver(norm_imp).saveAsPng(targetpath) except: print "Could not load or process file:", sourcepath print sys.exc_info() # Stategy #1: nested directories with os.listdir and os.path.isdir def processDirectory(theDir, fn): """ For every file in theDir, check if it is a directory, if so, invoke recursively. If not a directory, invoke 'loadProcessAndSave' on it. """ for filename in os.listdir(theDir): path = os.path.join(theDir, filename) if os.path.isdir(path): # Recursive call processDirectory(path, fn) else: loadProcessAndSave(path, fn) # Launch strategy 1: processDirectory(sourceDir, normalizeContrast)