This script batch processes all images with extension ".png" in the input directory.
Applies a median filter to each, and saves them in the output directory.
import ij.IJ
import ij.ImagePlus
import java.io.File
//
// Process files from input directory that match an extension.
//
batchProcessing(
inputDir = new File("my_input_dir"),
inputExtension = ".png",
outputDir = new File("my_output_dir"))
//-------------------------------------------------------------------------------------------------
/**
* Prepare output directory and process all images in the input directory matching extension
*/
def batchProcessing(inputDir: File,
inputExtension: String,
outputDir: File) {
val title = "Batch Process"
// Input directory
val inputDir = new File("my_input_dir")
if (!inputDir.exists) {
IJ.error(title, "Input directory does not exist: " + inputDir.getAbsolutePath)
return
}
// Output directory
val outputDir = new File("my_output_dir")
if (!outputDir.mkdirs()) {
IJ.error(title, "Failed to create output directory: " + outputDir.getAbsolutePath)
return
}
// List all files with extension ".png"
val inputFiles = inputDir.listFiles.filter(f => f.getName.endsWith(inputExtension))
// batch process iterating through all input files
inputFiles.foreach {
inputFile =>
println("Processing: " + inputFile.getAbsolutePath)
val src = IJ.openImage(inputFile.getPath)
val dest = process(src)
val outputFile = new File(outputDir, inputFile.getName)
IJ.saveAs(dest, "tif", outputFile.getAbsolutePath)
}
}
/**
* Custom image processing method
* @param src input image
* @return processed image
*/
def process(src: ImagePlus): ImagePlus = {
// Do some processing
IJ.run(src, "Median...", "radius=4")
// Return modified input
src
}