现有一个列表其中记录了多张tif影像的路径使用python的gdal实现将该列表内的所有tif进行拼接
要使用Python的gdal库将多个tif影像拼接在一起,可以按照以下步骤进行操作:
- 导入所需的库:
import os
from osgeo import gdal
- 定义一个函数来拼接tif影像:
def merge_tifs(tif_list, output_path):
# 打开第一个tif影像获取其宽度、高度和波段数
first_tif = gdal.Open(tif_list[0])
width = first_tif.RasterXSize
height = first_tif.RasterYSize
bands = first_tif.RasterCount
# 获取第一个tif影像的地理转换信息和投影信息
geotransform = first_tif.GetGeoTransform()
projection = first_tif.GetProjection()
# 创建输出影像
driver = gdal.GetDriverByName('GTiff')
output = driver.Create(output_path, width, height, bands, gdal.GDT_Float32)
# 设置输出影像的地理转换信息和投影信息
output.SetGeoTransform(geotransform)
output.SetProjection(projection)
# 逐个读取每个tif影像的波段数据,写入到输出影像中
for tif_path in tif_list:
tif = gdal.Open(tif_path)
for band in range(1, bands+1):
band_data = tif.GetRasterBand(band).ReadAsArray()
output.GetRasterBand(band).WriteArray(band_data)
# 关闭影像文件
output = None
- 定义一个包含tif影像路径的列表:
tif_list = ['/path/to/tif1.tif', '/path/to/tif2.tif', '/path/to/tif3.tif']
- 调用拼接函数,指定输出路径:
output_path = '/path/to/output.tif'
merge_tifs(tif_list, output_path)
通过上述步骤,可以使用gdal库将列表中的所有tif影像拼接在一起,并保存为指定的输出影像。
原文地址: https://www.cveoy.top/t/topic/i5Ii 著作权归作者所有。请勿转载和采集!