Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active February 24, 2024 12:27
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lacan/16e12482b52f539795e49cb2122060cc to your computer and use it in GitHub Desktop.
Save lacan/16e12482b52f539795e49cb2122060cc to your computer and use it in GitHub Desktop.
Extract multi-series file and resave as TIFF stacks or slices #Fiji #ImageJ #Macro #BIOP
/*
* Complex Format EXPORT MACRO
* By Olivier Burri @ EPFL - SV - PTECH - BIOP
* Given a folder, extracts all series inside all multi-file files with given extension in new folders
* Last edit: 13.02.2017
*/
////////////////////// SET PARAMETERS //////////////////////
////////////////////////////////////////////////////////////
// Set the extension you would like this macro to work with.
// Do not add a . at the beggining
extension = "czi"; //eg "lif", "vsi", etc...
// Set to true if you want all planes of the image to be saved individually
// if set to false, it will save each series as a stack.
is_save_individual_planes = true; // set to either true or false
// Padding for the naming of the series if you want to save all
// Images individually
pad = 3; // 0 means no padding. 2 means '01', '02' etc...
//////////////////// END SET PARAMETERS ////////////////////
////////////////////////////////////////////////////////////
// Beggining of macro. You should now have anything to edit after this line.
dir = getDirectory("Select a directory containing one or several ."+extension+" files.");
files = getFileList(dir);
setBatchMode(true);
k=0;
n=0;
run("Bio-Formats Macro Extensions");
for(f=0; f<files.length; f++) {
if(endsWith(files[f], "."+extension)) {
k++;
id = dir+files[f];
Ext.setId(id);
Ext.getSeriesCount(seriesCount);
print(seriesCount+" series in "+id);
n+=seriesCount;
for (i=0; i<seriesCount; i++) {
run("Bio-Formats Importer", "open=["+id+"] color_mode=Default view=Hyperstack stack_order=XYCZT series_"+(i+1));
fullName = getTitle();
dirName = substring(fullName, 0,lastIndexOf(fullName, "."+extension));
fileName = substring(fullName, lastIndexOf(fullName, " - ")+3, lengthOf(fullName));
File.makeDirectory(dir+File.separator+dirName+File.separator);
print("Saving "+fileName+" under "+dir+File.separator+dirName);
getDimensions(x,y,c,z,t);
if(is_save_individual_planes) {
save_string = getSaveString(pad);
print(dir+File.separator+dirName+File.separator+fileName+"_"+save_string+".tif");
run("Image Sequence... ", "format=TIFF name=["+fileName+"] digits="+pad+" save=["+dir+File.separator+dirName+File.separator+"]");
} else {
saveAs("tiff", dir+File.separator+dirName+File.separator+fileName+"_"+(i+1)+".tif");
}
run("Close All");
}
}
}
Ext.close();
setBatchMode(false);
showMessage("Done with "+k+" files and "+n+" series!");
function getSaveString(pad) {
str ="";
getDimensions(x,y,c,z,t);
if(t > 1) str+="t_"+IJ.pad(1,pad);
if(z > 1) str+="z_"+IJ.pad(1,pad);
if(c > 1) str+="c_"+IJ.pad(1,pad);
return str;
}
@cfausto
Copy link

cfausto commented Feb 22, 2024

Hello!

Thank you so much for making this script! It has helped me tremendously in processing large datasets. I recently updated FIJI to the newest version, and am now getting an error. I was wondering if you can help me address this?

Specifically in line 54:
dirName = substring(fullName, 0,lastIndexOf(fullName, "."+extension));

I am now getting this error message attached below, with the following debug window. I am using ImageJ version 2.14.0/1.54f Build: c89e8500e4

Screenshot 2024-02-22 at 2 37 55 PM
Screenshot 2024-02-22 at 2 38 12 PM

@lacan
Copy link
Author

lacan commented Feb 23, 2024

perhaps you put a dot when specifying the extension? or the image is the wrong extension?

@cfausto
Copy link

cfausto commented Feb 23, 2024

@lacan I appreciate that you are still active on this post!

I was just able to find the issue when manually saving at tifs last night, my .lif file name was too long and it created ellipsis! I guess FIJI does have a character limit for file naming. I was able to get it running after making the file name shorter. Good to know!

image

@lacan
Copy link
Author

lacan commented Feb 24, 2024

good to know. the ellipsis might me talked to a Mac file path issue, as Fiji is just reading the filename in the macro. thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment