Saving compressed tiff from Fiji/ImageJ

When saving images to compressed TIFF format in Fiji, there’s the way via Bio-Formats Exporter that offers the following compression formats:

image

(see also this related topic: Save compressed Tiff in ImageJ)


I noticed that in the source code, there seem to be a few more implementations that aren’t exposed in the GUI:

As I was planning to access the Java API via scripting (e.g. in Groovy) anyhow, I was searching for examples how to write to compressed tiff via the Bio-Formats and/or SCIFIO APIs, and ended up looking at this code in a test class for the different compression options:

Is saving via a byte[] array the way to go, or are there any higher-level API calls available? Are there any more concise examples how to save compressed images via the Java API (maybe in another gist by @ctrueden :slight_smile:)?

I would also be interested in an example using SCIFIO API, if that exists somewhere.

2 Likes

I was wondering the same at some point as it was quite tedious going via the byte[] where one even has to deal with the endianess and reshuffle the bytes. I have some code examples for this if you need.

After some more research, I found this (hacky) way of calling LociExporter with the macro option string:


With SCIFIO API, it should be a little more concise:

#@ Dataset dataset
#@ File (style="save") outputFile
#@ DatasetIOService datasetIOService

import io.scif.config.SCIFIOConfig
import io.scif.codec.CompressionType
import org.scijava.io.location.FileLocation

// EDIT: WRONG! This only works by chance just for LZW; see pull request linked below
// config = new SCIFIOConfig().writerSetCompression(CompressionType.LZW.toString())

config = new SCIFIOConfig().writerSetCompression(CompressionType.LZW.getCompression())
datasetIOService.save(dataset, new FileLocation(outputFile), config)

The available compression options are in CompressionType:

I’m not entirely sure how that relates to these other (more numerous) options in TiffCompression though:

I ended up enabling Deflate (zlib) compression support in #scifio with this pull request:

The zlib codec implementation was already present in scifio, but the TIFFFormat.Writer class wasn’t making use of it.


Just for the record:

I figured that the TiffCompression class encodes all the options for reading IFD tags in TIFF images, while CompressionType defines the possible options for writing.

2 Likes

Thanks to @imagejan’s efforts, SCIFIO 0.42.0 with zlib compression support, as well as fixed usage of the codecs in general, is now released and included with Fiji.

1 Like