-
Notifications
You must be signed in to change notification settings - Fork 2
/
BatchConvertNrrdsToJpgsRespectingFolders.txt
113 lines (100 loc) · 2.86 KB
/
BatchConvertNrrdsToJpgsRespectingFolders.txt
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
// "BatchConvertStacksToZProjJPGS"
//
// This macro batch processes all the nrrd files
// in a folder and any subfolders in that folder.
// It then Z projects them and saves them in JPG format
// ie some/image/path/image.nrrd -> some/image/path/image.jpg
// Takes up to 4 arguments
// <indir/file> <outdir> [informat] [outformat]
// informat should be the suffix of the input file eg nrrd, pic etc
// outformat should be the suffix of the output file eg jpg, png, tiff
// for output formats see documentation for saveAs at:
// http://rsbweb.nih.gov/ij/developer/macro/functions.html#S
// Adapted by Greg Jefferis from code at
// http://rsb.info.nih.gov/ij/macros/BatchProcessFolders.txt
requires("1.42k");
file = getArgument;
dir="";
outputDir="";
saveformat="jpg";
informat="nrrd";
// Process arguments if we were given any
if (file!=""){
arg = split(file,",");
// Must specify 2 or 3 arguments
if (arg.length>3)
saveformat=arg[3];
if (arg.length>2)
informat=arg[2];
else if(arg.length!=2)
exit();
// Must specify input and output dirs
if(arg[0]=="" || arg[1]=="") exit();
outputDir=arg[1];
if(!endsWith(outputDir,"/")) outputDir=outputDir+"/";
if(File.isDirectory(arg[0])) {
// we're dealing with a directory
dir=arg[0];
if(!endsWith(dir,"/")) dir=dir+"/";
} else {
// single file
dir=File.getParent(arg[0])+"/";
file=File.getName(arg[0]);
processFile(dir,outputDir,file);
exit();
}
}
// ... else ask for user input
if(dir=="") dir = getDirectory("Choose a stacks directory");
if(outputDir=="") outputDir = getDirectory("Choose output directory");
setBatchMode(true);
count = 0;
countFiles(dir);
print("Total files: "+count);
n = 0;
processFiles(dir, outputDir);
function countFiles(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
countFiles(""+dir+list[i]);
else
count++;
}
}
function processFiles(dir,outputDir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/")){
if(File.exists(""+outputDir+list[i]) != 1){
File.makeDirectory(""+outputDir+list[i]);
}
processFiles(""+dir+list[i], ""+outputDir+list[i]);
} else {
showProgress(n++, count);
processFile(dir,outputDir,list[i]);
}
}
}
function processFile(dir,outputDir,file) {
if (endsWith(file,"."+informat)) {
path = dir+file;
newpath=outputDir+substring(file,0,lengthOf(file)-lengthOf(informat))+saveformat;
if(File.exists(newpath) &&
File.lastModified(newpath) > File.lastModified(path) ){
print("Skipping file: "+file+" since "+saveformat+" already exists and is newer than input");
return;
}
print("Processing image: "+path+"\n");
open(path);
processImage();
saveAs(saveformat, newpath);
close();
close();
}
}
function processImage() {
run("Z Project...", "projection=[Standard Deviation]");
run("8-bit");
}