path = r"C:\Users\huangs\Desktop\test\inpath\\" files = glob(path+"*.tif") print(files) outpath = r"C:\Users\huangs\Desktop\test\outpath\\" for file in files: with rasterio.open(file) as src_dataset: profiles = src_dataset.profile band = src_dataset.read(1) # 目标值更新 for value in [10,20,30,40]: band_up = np.array(band) band_up[band_up!=value] = 0 # 设置输出文件路径,压缩格式可以选择'DEFLATE' outfile = outpath+os.path.basename(file).replace(".tif","_"+str(value)+".tif") profiles.update(nodata=0,compress='LZW') with rasterio.open(outfile, 'w', **profiles) as dst: dst.write(band_up, 1)