[GIS] How to import multiple tif images and save it as one tiff file with skimage or gdal in python

gdalmatplotlibnumpypythonscipy.spatial

I am new to Python and I am trying to import multiple tif images so that I can do image processing with skimage. I want to do image processing on a Landsat image to define linear features for a project that has to be in before the end of the year.

I am using a Landsat 8 image that consists of 11 bands (individual tif files), a bqa file and a metadata.txt file.

I can import a band one by one, but I don't know how to stack the bands into one tiff file that also contains the metadata.txt file.

This is what I have done so far:

import os filename = os.path.join('path', 'band1.TIF') 
import skimage from skimage 
import io bandone = io.imread(filename)

So my image has been successfully imported as converted to a nparray.
I have to repeat this to import all the bands.

What is the best way to save these bands together?

skimage.external.tifffile.imsave('file, data, **kwargs) ?
skimage.io.imsave(fname, arr, plugin=None, **plugin_args) ?
TiffWriter ?

Or should I do something else completely?

Please elaborate on the details (such as how to define **kwargs for instance).

Best Answer

It is easy with gdal_array.SaveArray:

from osgeo import gdal, gdal_array

# open one of the landsat images to use as a prototype to get projection info from
ds = gdal.Open("landsat_band.tif")

# list of np arrays to stack
bands = [band1_arr, band2_arr, band3_arr ... ]

# stack arrays
stack = np.array(bands)

# save as output.tif
gdal_array.SaveArray(stack, "output.tif", "gtiff", prototype=ds)

ds = None
Related Question