0

I have python code doing in principle:

import tifffile
data = np.empty((l, m, n), dtype=np.float32)
tifffile.imsave(file_name, data, imagej=True)

which produces a tiff-file in a format that works further down the line. However this can be quite memory intensive. Therefore I want to write something like:

writer = tifffile.TiffWriter(file_name, imagej=True)
for page in data:
    writer.write(page)

This produces a tiff-file too, but the format is different than in the example above.

I believe tifffile.imsave is just a wrapper for tifffile.TiffWriter.write. What options do I need to use to reproduce the same result with TiffWriter that I get with imsave

Michael
  • 1,464
  • 1
  • 20
  • 40
  • 1
    None of the above code works and actually raises ValueErrors. The ImageJ format does not support float64 data and requires the shape of the data to be known when writing the first page. Try the [memmap](https://github.com/cgohlke/tifffile/blob/90805c33065da10d8e604850a840c3166099f91f/tifffile/tifffile.py#L574-L593) approach with float32. It should work with the ImageJ format. – cgohlke Mar 02 '22 at 14:56
  • @cgohlke sorry I forgot the dtype, will add it – Michael Mar 02 '22 at 15:09
  • Using ```writer.write(page, planarconfig='contig')``` seems to work – Michael Mar 02 '22 at 15:15
  • 1
    I don't know which version of tifffile was used, but `planarconfig='contig'` is simply ignored for the shape (m, n) images and the code raises a ValueError as expected when writing ImageJ files page by page. Use the memmap approach. – cgohlke Mar 02 '22 at 16:21
  • This works: (I put it in an answer) – Michael Mar 03 '22 at 09:39

1 Answers1

0

This works:

memmap = tifffile.memmap(file_name, shape=data.shape, dtype=data.dtype, imagej=True)
for i, page in enumerate(data):
    memmap[i, :, :] = img
memmap.flush()
Michael
  • 1,464
  • 1
  • 20
  • 40