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).

  • Did you try gdal_merge.py? www.gdal.org/gdal_merge.html – Zoltan Nov 3 '16 at 7:45
  • 1
    With option -separate. You need to convert all bands to same resolution first. – user30184 Nov 3 '16 at 7:55
  • Hey Zoltan, no I havn't. I'll take a look. Thanks! – Gab Nov 3 '16 at 7:57
  • When I convert all the bands to the same resolution, isn't that going to affect the quality of my data? – Gab Nov 3 '16 at 9:00
  • How do I assign directories to all my image files? When I use $ gdalinfo can only take one file at a time. So then $ gdal_merge.py -o out.tif inbandone.TIF works but only for bandone. How do I get gdal_merge.py -o out.tif in1.tif in2.tif inn.tif to work? – Gab Nov 3 '16 at 9:09

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.