Image dimensions from ImageJ
Below is a ImageJ Python script that will read a user-provided image file and output the width and height, with units, to stdout. This macro is designed to be called from the command line using the ImageJ executable. With my Mac OSX computer running Fiji, the path is
/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx. This has not been tested elsewhere and may not work without some effort. It relies on the Bio-Formats plugin to read the file and was written to convert from Zeiss’s .czi files, so no guarantee that it works with others as desired. It is especially important to note that this does not set the scale, but infers it based on the metadata stored in the .czi files. Therefore, it will probably not work well with other file types.
/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless get_image_dims.py input
#!/usr/bin/env python import sys from loci.plugins import BF infile = sys.argv imps = BF.openImagePlus(infile) imp = imps cal = imp.getCalibration() out_list =  out_list.append(infile) out_list.append(cal.pixelWidth * imp.getDimensions()) out_list.append(cal.pixelHeight * imp.getDimensions()) out_list.append(cal.unit) sys.stdout.write('\t'.join(map(str, out_list)) + '\n')
It is easy to batch process a set of files using simple Shell scripting. Here is an example that will process all .czi files in a directory. Given my installation always throws up a bunch of Java errors to stderr, I decided just to direct that to
for i in *.czi; do /Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --headless get_image_dims.py $i 2> /dev/null; done
Here are some links to pages where I extracted information useful for creating this script.
- Extract calibration
- Reading data with Bio-Format
- General scripting with Fiji
- Calling script from command-line