How to extract series from unique .czi file

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?



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


 * 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() );

run("Bio-Formats Macro Extensions");
for(f=0; f<files.length; f++) {
	if(endsWith(files[f], ".czi")) {
		id = dir+files[f];
		print(seriesCount+" series in "+id);
		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));
			savingDir = dir+File.separator+dirName+File.separator;
			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");

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;
1 Like

Thank you so much! I will try it immediately!

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:
	dstDir = DirectoryChooser("Output directory").getDirectory()
	if not dstDir:
	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"
		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"
			print "File saved as {}".format(imp.title)


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…

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?


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…


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.



1 Like

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!