直接上代码
import numpy as np
from osgeo import gdal
from matplotlib import pyplot as plt
import numpy.ma as ma
data = gdal.Open("E:\data\L9.tif")
arr = data.ReadAsArray()
bandRed = arr[3].astype(float)
bandIFR = arr[4].astype(float)
mask1 = bandRed == 0
mask2 = bandIFR == 0
bandRedm = ma.array(bandRed,mask = mask1,fill_value=None)
bandIFRm = ma.array(bandIFR,mask = mask2,fill_value=None)
NDVI = (bandIFRm-bandRedm)/(bandRedm+bandIFRm)
outfile1 = "E:\data\L9_NDVI.tif"
driver = gdal.GetDriverByName('GTiff')
output1 = driver.Create(outfile1,xsize = 7671,ysize = 7781,bands = 1,eType = gdal.GDT_Float64)
output1.WriteArray(NDVI, xoff = 0, yoff = 0, interleave = 'band')
del output1