Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python. How to save a 4 dimension array (21,32,1024,1024) as a tif image. biomedicine

I want to save an stack of arrays in a tiff file, so a microscopy software can read it in channels and z planes, as images. So this is a 4 dimensions array: (21,32,1024,1024). But I do not find a way.

For example using: io.imsave(os.path.join(outpath), stack2), this is saved as a stack of individual images but not in grups of 32 channels representing the 21 z planes.

do you know any way to achieve that?


1 Answers

Since the TIFF specification does not handle multi-channel Z stacks, additional metadata needs to be saved with the image data. There are two common metadata formats for saving ZCYX images in TIFF for bio-imaging: OME-TIFF and ImageJ hyperstacks. OME-TIFF is supported by more software. Tifffile can read and write both formats:

import numpy
from tifffile import imwrite

image = numpy.zeros((21, 32, 1024, 1024), dtype='uint16')

# write OME-TIFF
imwrite('zcyx.ome.tif', image)

# write ImageJ hyperstack
imwrite('zcyx.tif', image, imagej=True)
like image 106
cgohlke Avatar answered Nov 30 '25 00:11

cgohlke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!