I am working with 4 band GeoTIFF images. I used GDAL to open these images. Each layer needs pre-processing and after completion of processing, I need to write back into a TIFF file. I used the below code, but I couldn't get all the bands together. When I check the written file, it contains only a single band.
for data_file in os.listdir(destSAR):
data = data_file.split('_')
joinStr = [data[6],data[7],data[8],data[9]]
joinedName = '_'.join(joinStr)
imgSAR = joinSAR+joinedName
imgEO = joinEO+joinedName
SARt = gdal.Open(imgSAR)
opref = cv2.imread(imgEO)
SAR=SARt.ReadAsArray()
output=getSARmask(SAR,opref) #processing the SAR images
os.chdir(outputPath)
filename = 'post_process_'+joinedName
# print(len(output)) # have 4 bands in output
for i in range(len(output)):
ds = SARt #original SAR image with 4 bands, using for Geo information
arr = output[i]
# arr = band.ReadAsArray()
[cols, rows] = arr.shape
arr_min = arr.min()
arr_max = arr.max()
arr_mean = int(arr.mean())
arr_out = np.where((arr < arr_mean), 10000, arr)
driver = gdal.GetDriverByName("GTiff")
outdata = driver.Create(filename, rows, cols, 1, gdal.GDT_UInt16)
outdata.SetGeoTransform(ds.GetGeoTransform())##sets same geotransform as input
outdata.SetProjection(ds.GetProjection())##sets same projection as input
outdata.GetRasterBand(1).WriteArray(arr_out)
outdata.GetRasterBand(1).SetNoDataValue(10000)##if you want these values transparent
outdata.FlushCache() ##saves to disk!!
outdata = None
band=None
ds=None