-
Notifications
You must be signed in to change notification settings - Fork 218
/
BatchConverter.java
156 lines (150 loc) · 5.12 KB
/
BatchConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.util.Tools;
import ij.io.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/** This plugin implements the File/ /Convert command,
which converts the images in a folder to a specified format. */
public class BatchConverter implements PlugIn, ActionListener {
private static final String[] formats = {"TIFF", "8-bit TIFF", "JPEG", "GIF", "PNG", "PGM", "BMP", "FITS", "Text Image", "ZIP", "Raw"};
private static String format = formats[0];
private static double scale = 1.0;
private static boolean useBioFormats;
private static int interpolationMethod = ImageProcessor.BILINEAR;
private static boolean averageWhenDownSizing;
private String[] methods = ImageProcessor.getInterpolationMethods();
private Button input, output;
private TextField inputDir, outputDir;
private GenericDialog gd;
public void run(String arg) {
if (!showDialog())
return;
String inputPath = inputDir.getText();
if (inputPath.equals("")) {
IJ.error("Batch Converter", "Please choose an input folder");
return;
}
String outputPath = outputDir.getText();
if (outputPath.equals("")) {
IJ.error("Batch Converter", "Please choose an output folder");
return;
}
File f1 = new File(inputPath);
if (!f1.exists() || !f1.isDirectory()) {
IJ.error("Batch Converter", "Input does not exist or is not a folder\n \n"+inputPath);
return;
}
File f2 = new File(outputPath);
if (!outputPath.equals("") && (!f2.exists() || !f2.isDirectory())) {
IJ.error("Batch Converter", "Output does not exist or is not a folder\n \n"+outputPath);
return;
}
String[] list = (new File(inputPath)).list();
IJ.resetEscape();
Opener opener = new Opener();
opener.setSilentMode(true);
long t0 = System.currentTimeMillis();
for (int i=0; i<list.length; i++) {
if (IJ.escapePressed())
break;
if (IJ.debugMode) IJ.log(i+" "+list[i]);
String path = inputPath + list[i];
if ((new File(path)).isDirectory())
continue;
if (list[i].startsWith(".")||list[i].endsWith(".avi")||list[i].endsWith(".AVI"))
continue;
IJ.showStatus(i+"/"+list.length);
IJ.showProgress(i+1, list.length);
ImagePlus imp = null;
IJ.redirectErrorMessages(true);
if (useBioFormats)
imp = Opener.openUsingBioFormats(path);
else
imp = opener.openImage(inputPath,list[i]);
IJ.redirectErrorMessages(false);
if (imp==null) {
String reader = useBioFormats?"Bio-Formats not found or":"IJ.openImage()";
IJ.log(reader+" returned null: "+path);
continue;
}
if (scale!=1.0) {
int width = (int)(scale*imp.getWidth());
int height = (int)(scale*imp.getHeight());
ImageProcessor ip = imp.getProcessor();
ip.setInterpolationMethod(interpolationMethod);
ip.setProgressBar(null);
imp.setProcessor(null, ip.resize(width,height,averageWhenDownSizing));
ip = null;
}
if (format.equals("8-bit TIFF") || format.equals("GIF")) {
if (imp.getBitDepth()==24)
IJ.run(imp, "8-bit Color", "number=256");
else
IJ.run(imp, "8-bit", "");
}
IJ.saveAs(imp, format, outputPath+list[i]);
imp.close();
imp = null;
}
IJ.showStatus(list.length+" files converted in "+IJ.d2s((System.currentTimeMillis()-t0)/1000.0,2)+" seconds");
IJ.showProgress(1,1);
Prefs.set("batch.input", inputDir.getText());
Prefs.set("batch.output", outputDir.getText());
}
private boolean showDialog() {
gd = new GenericDialog("Batch Convert");
addPanels(gd);
gd.setInsets(15, 0, 5);
gd.addChoice("Output_format:", formats, format);
gd.addChoice("Interpolation:", methods, methods[interpolationMethod]);
//gd.addStringField("Height (pixels): ", height==0?"\u2014":""+height, 6);
gd.addNumericField("Scale factor:", scale, 2);
gd.addCheckbox("Average when downsizing", averageWhenDownSizing);
gd.addCheckbox("Read images using Bio-Formats", useBioFormats);
gd.setOKLabel("Convert");
gd.showDialog();
if (gd.wasCanceled())
return false;
format = gd.getNextChoice();
interpolationMethod = gd.getNextChoiceIndex();
//height = (int)Tools.parseDouble(gd.getNextString(), 0.0);
scale = gd.getNextNumber();
averageWhenDownSizing = gd.getNextBoolean();
useBioFormats = gd.getNextBoolean();
return true;
}
void addPanels(GenericDialog gd) {
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
input = new Button("Input...");
input.addActionListener(this);
p.add(input);
inputDir = new TextField(Prefs.get("batch.input", ""), 45);
p.add(inputDir);
gd.addPanel(p);
p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
output = new Button("Output...");
output.addActionListener(this);
p.add(output);
outputDir = new TextField(Prefs.get("batch.output", ""), 45);
p.add(outputDir);
gd.addPanel(p);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String s = source==input?"Input":"Output";
String path = IJ.getDirectory(s+" Folder");
if (path==null) return;
if (source==input)
inputDir.setText(path);
else
outputDir.setText(path);
if (IJ.isMacOSX())
{gd.setVisible(false); gd.setVisible(true);}
}
}