1+ /*
2+ * ImageJ Plugins
3+ * Copyright (C) 2002-2016 Jarek Sacha
4+ * Author's email: jpsacha at gmail dot com
5+ *
6+ * This library is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 2.1 of the License, or (at your option) any later version.
10+ *
11+ * This library is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public
17+ * License along with this library; if not, write to the Free Software
18+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+ *
20+ * Latest release available at https://github.com/ij-plugins
21+ */
22+
23+ import java .io .File
24+
25+ import ij .{IJ , ImagePlus }
26+
27+ //
28+ // Batch process images applying a median filter.
29+ //
30+ batchProcess(
31+ filter = {
32+ imp =>
33+ IJ .run(imp, " Median..." , " radius=4" )
34+ imp
35+ },
36+ inputDir = new File (" my_input_dir" ),
37+ inputExtension = " .png" ,
38+ outputDir = new File (" my_output_dir" )
39+ )
40+
41+ // -------------------------------------------------------------------------------------------------
42+
43+
44+ /**
45+ * Apply filter to files in input directory, save modified files in the output directory.
46+ *
47+ * @param filter operation to be applied to processed images
48+ * @param inputDir input directory
49+ * @param inputExtension input file name extension
50+ * @param outputDir output directory
51+ */
52+ def batchProcess (inputDir : File ,
53+ inputExtension : String ,
54+ outputDir : File ,
55+ filter : ImagePlus => ImagePlus ) {
56+ val title = " Batch Process"
57+
58+ // Input directory
59+ if (! inputDir.exists) {
60+ IJ .error(title, " Input directory does not exist: " + inputDir.getAbsolutePath)
61+ return
62+ }
63+
64+ // Output directory
65+ if (! outputDir.mkdirs()) {
66+ IJ .error(title, " Failed to create output directory: " + outputDir.getAbsolutePath)
67+ return
68+ }
69+
70+ // List all files with extension ".png"
71+ val inputFiles = inputDir.listFiles.filter(_.getName.endsWith(inputExtension))
72+
73+ // batch process iterating through all input files
74+ for (inputFile <- inputFiles) {
75+ println(" Processing: " + inputFile.getAbsolutePath)
76+ val src = IJ .openImage(inputFile.getPath)
77+ val dest = filter(src)
78+ val outputFile = new File (outputDir, inputFile.getName)
79+ IJ .saveAs(dest, " tif" , outputFile.getAbsolutePath)
80+ }
81+ }
0 commit comments