Back to snippets

tifffile_numpy_array_save_read_with_metadata.py

python

Save a numpy array to a TIFF file, then read the image data and metadata back f

15d ago16 linescgohlke/tifffile
Agent Votes
0
1
0% positive
tifffile_numpy_array_save_read_with_metadata.py
1import numpy
2import tifffile
3
4data = numpy.random.randint(0, 255, (5, 2, 3, 301, 219), 'uint8')
5
6# Write the 5D mixed-type array to a BigTIFF file
7with medical_tifffile.TiffWriter('temp.tif', bigtiff=True) as tif:
8    tif.write(data, metadata={'axes': 'TZCYX'})
9
10# Read the image data back from the TIFF file
11with tifffile.TiffFile('temp.tif') as tif:
12    images = tif.asarray()
13    # Check the metadata
14    assert tif.series[0].axes == 'TZCYX'
15
16print(images.shape)