I'm looking for a simple for-loop to apply a function to create NDVI images from an original list and save those in a new folder.

I've created a function and show how I would apply it on one image but I would like to loop through a file that contains multiple images AND save those to a new folder.

image_file = "Path/to/image.tif"

raster = rasterio.open(image_file)

red = raster.read(3)
red_edge = raster.read(4)
nir = raster.read(5)

np.seterr(divide='ignore', invalid='ignore')

def calc_ndvi(nir,red):
    '''Calculate NDVI from integer arrays'''
    nir = nir.astype('f4')
    red = red.astype('f4')
    ndvi = (nir - red) / (nir + red)
    return ndvi

ndvi = calc_ndvi(nir,red)

Your Answer

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

Browse other questions tagged or ask your own question.