Ethan
Ethan

Reputation: 1067

Open tiff image stack in python with gdal

I'm trying to open a tiff stack using gdal in python. It is a single file, within which are mutliple tiff files. When I open it in ImageJ, I can scroll through the stack. But doing "gdal.Open(file).ReadAsArray() only loads the first tiff file. Is there a way I can load the whole stack?

Upvotes: 0

Views: 2585

Answers (1)

cgohlke
cgohlke

Reputation: 9437

Iterate over GetSubDatasets and (in case all TIFF pages have the same dimensions) create a single numpy array:

import numpy, gdal, gdalconst
dataset = gdal.Open("multipage.tiff", gdalconst.GA_ReadOnly)
data = numpy.array([gdal.Open(name, gdalconst.GA_ReadOnly).ReadAsArray()
                    for name, descr in dataset.GetSubDatasets()])

Upvotes: 2

Related Questions