import os import rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling # ===================== 仅修改路径即可 ===================== # 1. 输入文件夹(原始TIFF所在路径) INPUT_DIR = r"/.../your/input/path/" # 2. 输出文件夹(重采样后的文件保存路径) OUTPUT_DIR = r"/.../your/output/path/" # 3. 分辨率根据自己需要修改,这里我设置0.01度 TARGET_RES = 0.01 # 分辨率:0.01度 RESAMPLE_METHOD = Resampling.bilinear # ====================================================================== def resample_tif(input_path: str, output_path: str): with rasterio.open(input_path) as src: transform, width, height = calculate_default_transform( src.crs, src.crs, src.width, src.height, *src.bounds, resolution=TARGET_RES ) out_meta = src.meta.copy() out_meta.update({ 'crs': src.crs, 'transform': transform, 'width': width, 'height': height, 'dtype': src.dtypes[0] }) with rasterio.open(output_path, 'w', **out_meta) as dst: for i in range(1, src.count + 1): reproject( source=rasterio.band(src, i), destination=rasterio.band(dst, i), src_transform=src.transform, src_crs=src.crs, dst_transform=transform, dst_crs=src.crs, resampling=RESAMPLE_METHOD ) print(f"✅ 完成:{os.path.basename(output_path)}") if __name__ == '__main__': # 自动创建输出文件夹(不存在则创建,存在不报错) os.makedirs(OUTPUT_DIR, exist_ok=True) tif_files = [f for f in os.listdir(INPUT_DIR) if f.lower().endswith('.tif')] if not tif_files: print("❌ 输入文件夹未找到TIFF文件!") exit() # 批量处理所有.tif文件 print(f"📂 开始处理,共找到 {len(tif_files)} 个TIFF文件...") for tif_name in tif_files: # 拼接输入/输出完整路径 input_path = os.path.join(INPUT_DIR, tif_name) # 输出文件名:原文件名 + 后缀 out_name = os.path.splitext(tif_name)[0] + ".tif" output_path = os.path.join(OUTPUT_DIR, out_name) # 执行重采样 resample_tif(input_path, output_path) print(f"\n🎉 所有文件重采样完成!结果保存在:{OUTPUT_DIR}")