FileInfo update when saving ImagePlus.GRAY16 instance as Jpeg


I use ImageJ API in my java development work. I would like to use path) method to save an ImagePlus instance as a Jpeg file. My method call looks like this:

new FileSaver(myImagePlusInstance).saveAsJpeg(myPathToFile);

However, I have a problem with the saveAsJpeg(String path) method. For some reason (that I do not know) its implementation when saving ImagePlus.GRAY16 and ImagePlus.GRAY32 image instances explicitly excludes them from updating their corresponding FileInfo object instances:

/** Save the image in JPEG format using the specified path.
	@see #setJpegQuality
	@see #getJpegQuality
public boolean saveAsJpeg(String path) {
	String err =, path, jpegQuality);
	if (err==null && !(imp.getType()==ImagePlus.GRAY16 || imp.getType()==ImagePlus.GRAY32))
		updateImp(fi, FileInfo.GIF_OR_JPG);
	return true;

I need to save as Jpeg ImagePlus.GRAY16 images. Further in my java application I have to use getOriginalFileInfo().directory field for my ImagePlus instances.

What is the correct way to update the FileInfo object instance (in particular, its directory field) when saving an ImagePlus.GRAY16 image as Jpeg file using ImageJ API?

@croset3 The logic in question was introduced in this commit on 2009-Jun-09. I am afraid that there is no explanation of why it is done that way; maybe @Wayne remembers the rationale.

You could also try changing the code yourself and see whether it works for you. And submit a patch to ImageJ1 if so.

The FIleSaver.saveAsJpeg() method is working as expected. It assumes 16-bit and 32-bit being saved in JPEG format are being exported for publication so it does not update the FileInfo. The FileInfo will be updated if you save in TIFF format.

1 Like

Thank you for taking time to reply. So, having your design and implementation as they are, what method from the ImageJ API do I have to use if I need to update FileInfo on 16-bit and 32-bit Jpegs saved with FIleSaver(…).saveAsJpeg(…) method?

1 Like

The ImagePlus.getOriginalFileInfo() method returns a reference to the FileInfo, so you can easily update it, as in the following JavaScript example, which updates the directory field.

imp = IJ.openImage("");
fi = imp.getOriginalFileInfo();
print(fi); = "xxxx";
fi = imp.getOriginalFileInfo();
1 Like