【小代码片】GDAL - 批量图像重采样
程序员文章站
2022-05-26 14:02:42
...
import os
from osgeo import gdal
def resize(input, output, factor):
# 适用于下采样
in_ds = gdal.Open(input)
out_rows = int(in_ds.RasterYSize / factor)
out_columns = int(in_ds.RasterXSize / factor)
num_bands = in_ds.RasterCount
gtiff_driver = gdal.GetDriverByName('GTiff')
out_ds = gtiff_driver.Create(output,
out_columns, out_rows, num_bands)
out_ds.SetProjection(in_ds.GetProjection())
geotransform = list(in_ds.GetGeoTransform())
geotransform[1] *= factor
geotransform[5] *= factor
out_ds.SetGeoTransform(geotransform)
data = in_ds.ReadRaster(
buf_xsize=out_columns, buf_ysize=out_rows)
out_ds.WriteRaster(0, 0, out_columns, out_rows, data)
out_ds.FlushCache()
for i in range(num_bands):
out_ds.GetRasterBand(i + 1).ComputeStatistics(False)
out_ds.BuildOverviews('average', [2, 4, 8, 16])
del out_ds
def getFileName(path):
# 获取指定目录下的指定后缀的文件名
f_list = os.listdir(path)
out_list = []
for i in f_list:
if os.path.splitext(i)[1] == '.tif':
out_list.append(i)
return out_list
if __name__ == '__main__':
root_dir = 'F:/Datasets/NEON_BLAN_2019/IMG_AHE/'
outdir = 'F:/Datasets/NEON_BLAN_2019/IMG_0.8/'
tif_list = getFileName(root_dir)
for name in tif_list:
pth = os.path.join(root_dir, name)
out_pth = os.path.join(outdir, name)
resize(pth, out_pth, factor=1.6)
factor 是像元变化的倍数
上一篇: tensorflow模型调用
下一篇: Markdown编辑器
推荐阅读