How to extract series from unique .czi file

imagej
czi
filereader

#1

Hello everybody,

I am dealing with czi files containing timeseries from different positions of a 96well plate. What I would like to do is to write a script, possibly Jython, to open each series (one series corresponds to one location and the file contains the whole timeseries for tha position) and save it as an independent file in .tiff format.
I am still learning how to use Jython and bioformats on script and I have no idea on how to use the ZeissCZIReader class… is there anybody who can help me?

Thanks!
Lucrezia


#2

Hello,

We wrote a macro do to this a while back. I am attaching it here in the hopes that it will help you.

All the best

Oli

/*
 * CZI EXPORT MACRO
 * By Olivier Burri @ EPFL - SV - PTECH - BIOP
 * Given a folder, extracts all series inside all CZI files in new folders 
 * Last edit: 26.02.2016
 */


dir = getDirectory("Select a directory containing one or several CZI files");

files = getFileList(dir);

print("Export : Starts at " + now() );

setBatchMode(true);
k=0;
n=0;
run("Bio-Formats Macro Extensions");
for(f=0; f<files.length; f++) {
	if(endsWith(files[f], ".czi")) {
		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 use_virtual_stack series_"+(i+1));
			//run("Bio-Formats Importer", "open=["++"] color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT use_virtual_stack series_1");
			fullName	= getTitle();
			dirName 	= substring(fullName, 0,lastIndexOf(fullName, ".czi"));
			fileName 	= substring(fullName, lastIndexOf(fullName, " - ")+3, lengthOf(fullName));
			print(fileName);
			savingDir = dir+File.separator+dirName+File.separator;
			
			File.makeDirectory(savingDir);
			
			print("Saving "+fileName+" under "+savingDir);
			
			
			//run("Bio-Formats Exporter", "save=["+savingDir+"pos_"+i+".ids]");
			print("Saving : Starts at " + now() );
			//run("Bio-Formats Exporter", "save=["+savingDir+"Pos_"+i+".ids]");
			saveAs("tiff", savingDir+dirName+File.separator+fileName+"_"+(i+1)+".tif");
			print("Saving :  Ends at " + now() );
			run("Close All");
			//close();
		}
	}
}
setBatchMode(false);

print("Export : Ends at " + now() );
showMessage("Done with "+k+" files and "+n+" series!");

function now(){
	getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
	nowValue = ""+IJ.pad(hour,2)+":"+IJ.pad(minute,2)+":"+IJ.pad(second,2);
	return nowValue;
}

#3

Thank you so much! I will try it immediately!


#4

Ok, I managed to split my big file and obtain single .czi files. Now, the problem is that I need them in .tiff format, because most of the script for processing that I have need this format.
It may sound silly, but I cannot write a script in Jython to save these files as tiff files. Here is my code:

def run():
	srcDir = DirectoryChooser("Input directory").getDirectory()
	if not srcDir:
		return
	dstDir = DirectoryChooser("Output directory").getDirectory()
	if not dstDir:
		return
	for root, directories, filenames in os.walk(srcDir):
		for filename in filenames:
			asTiff(srcDir, dstDir, filename)

def asTiff(srcDir, dstDir, filename):
	if not path.exists(dstDir) and not path.isdir(dstDir):
		print "The folder doesn´t exist"
	else:
		print "The folder {} exists".format(dstDir)
		print "Processing ", filename
		imp = IJ.openImage(os.path.join(srcDir, filename))
        if imp is None:
				raise Exception("Could not open image from file: ", filename)
        filepath = dstDir+filename
		if path.exists(filepath):
			print "The file already exists and it will not be overwritten"
		else:
			IJ.saveAsTiff(imp.filepath)
			print "File saved as {}".format(imp.title)

			
    
run()

I have an error when I try to open the file with openImage: the image is opened by bio-format (and I don´t want that), but imp results to be NoneType, so it raises an Exception.
I am not very esperienced with programming in general, I am still learning, so I don´t know how to solve this issue…


#5

I’m afraid I am confused. The script was supposed to give you the tiff files at the end (extension .tif). Do you mean the script I sent you did not work?

I don’t understand why you do not want bioformats to open it?

Looking at your code, I think there is a mistake in how you are saving the image.
IJ.saveAsTiff(imp.filepath) should read saveAsTiff(imp, filepath) perhaps…

But I would like to add that the code I gave you will save everything as a tiff directly from the CZI… Could you let me know what failed?

Best
Oli


#6

Hi Oli, you are right, sorry, I didn´t explain the full story:
I tried the macro you sent me, but I got the following error.

Honestly, I don´t know if I did anything wrong, I just did copy and paste. I didn´t want to bother you because in the meantime I also got access to a computer with the ZEN software that allowed me to separate the scenes as saparate .czi files, therefore I tried to focus on those files. In these days I tried to convert the files from .czi to .tif, but without much success. Here comes my new question.

When I say that I don´t want bio-formats to open up the file I mean that I would like to make it work in a Batch mode, which should go without saying when programming in Jython: I usually have to explicitly show the image. Instead, in this case, the first image is opened and then nothing else happen. Indeed, the problem is that I don´t even get to the point of saveAsTiff(), because the script stops running when I raise the exception if imp is None…

Cheers,
Lucrezia


#7

I see. Would it be possible for you to share one of your czi files (the big ones)? It seems that there is an issue with the way the script tries to pick up the file name. This would help me debug that part. In the mean time perhaps someone more versed in Jython can give you a hand with the other script.

Best

Oli


#8

Hi Oli,
I tried to attach a small file in .zip format, but have troubles loading.
In the meantime, I kind of “solved” the problem by trying to use Process/Batch/Convert command, even though I am now trying to automate it in my script. The funny thing is that, even though I provide the new coordinates in the script, the command recollets the input and output folders I wrote by hand while testing the command manually.
Anyway, thank you for the reply and help!

Best,
Lucrezia