Export an ImgLib2 image as TIFF

Dear community,

is there a simple way to save a 16-bit ImgLib2 ArrayImg to a TIFF?

I tried ImgSaver from SCIFIO, but the result is an 8-bit TIFF although my initial image is of the UnsignedShortType.

There might be a way to do it via some ImageJ functions, but I would not like to carry all the ImageJ related dependencies with the project just for the sake of saving a TIFF.

Thank you!

Dear @adibrov,

the following snippet saves an empty ArrayImg<UnsignedShortType> to a 16-bit TIFF file:

import io.scif.img.ImgSaver;

import net.imglib2.FinalInterval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.util.Intervals;

public class ArrayImgToTiffTest {

	public static void main(String[] args) {
		FinalInterval interval = Intervals.createMinMax(0, 100, 0, 100);

		ArrayImgFactory<UnsignedShortType> fac = new ArrayImgFactory<>();
		UnsignedShortType type = new UnsignedShortType();
		Img<UnsignedShortType> img = fac.create(interval, type);
		
		ImgSaver saver = new ImgSaver();
		String imgName = "tiffTest.tif";
		try {
			saver.saveImg(imgName, img);
		}
		catch (Exception exc) {
			exc.printStackTrace();
		}
	}
}

with the following TIFF header:

➜ tiffinfo tiffTest.tif
TIFF Directory at offset 0x8 (8)
  Image Width: 1 Image Length: 1
  Resolution: 10000, 10000 pixels/cm
  Bits/Sample: 16
  Sample Format: unsigned integer
  Compression Scheme: None
  Photometric Interpretation: min-is-black
  Samples/Pixel: 1
  Rows/Strip: 1
  Planar Configuration: single image plane
  ImageDescription: SCIFIO=0.28.0-SNAPSHOT
axes=X,Y
lengths=1,1
scales=1.0,1.0
units=null,null
bitsPerPixel=16
images=1
channels=1
slices=1
frames=1
hyperstack=true
mode=composite
unit=null

How do you create your ArrayImgs and what structure do they have?

Best,
Stefan

2 Likes