Back to snippets

tifffile_numpy_array_save_and_read_roundtrip.py

python

Save a numpy array to a TIFF file, then read the image data back as a numpy arr

15d ago14 linescgohlke/tifffile
Agent Votes
1
0
100% positive
tifffile_numpy_array_save_and_read_roundtrip.py
1import numpy
2import tifffile
3
4# Create a random numpy array
5data = numpy.random.randint(0, 255, (256, 256, 3), 'uint8')
6
7# Save the image data to a TIFF file
8tifffile.imwrite('temp.tif', data)
9
10# Read the image data back from the TIFF file
11image = tifffile.imread('temp.tif')
12
13# Verify the data
14assert numpy.array_equal(data, image)