I have a directory containing multiple .tif files which I want to reclassify and save to a new directory. The following code already worked for a single raster. Unfortunately, I don't know how to use
driver.CreateCopy to write multiple files.
in_directory = 'D:/Python/Copied' file = glob.glob(os.path.join(in_directory, '*.tif')) for data_path in file: raster = gdal.Open(data_path, gdal.GA_ReadOnly) myarray = raster.GetRasterBand(1).ReadAsArray() # modify numpy array to mask values myarray[myarray == 0] = 1 myarray[myarray > 1] = 0 # open output dataset, which is a copy of original driver = gdal.GetDriverByName('GTiff') ds_out = driver.CreateCopy("D:/Python/Copied/reclass/neu.tif", raster) # write the modified array to the raster ds_out.GetRasterBand(1).WriteArray(myarray) # set the NoData metadata flag ds_out.GetRasterBand(1).SetNoDataValue(0) # clear the buffer, and ensure file is written ds_out.FlushCache()