import os
from pathlib import Path
from collections import OrderedDict
import rasterio
#需要提前在final_data_path中创造band1,band2,band3,band4,band5,band6文件夹
NUM_BAND = 6
current_processing = 'val' #‘train’, ‘val’, ‘test’
ini_data_path = Path('D:\\STFdata\\zdd_data1\\lgc2400').joinpath(current_processing)
final_data_path ='D:\\STFdata\\zdd_data1\\单波段数据\\lgc2400\\'
image_dirs = [p for p in ini_data_path.glob('*') if p.is_dir()]
order = OrderedDict()
order[0] = '00_landsat'
order[1] = '00_modis'
order[2] = '01_landsat'
order[3] = '01_modis'
# path = 'E:\\STF_note\\08.时空融合神经网络代码\\EDCSTFN\\data_cia\\train\\2001_290_1017\\00_landsat.tif'
# file_name = Path('E:\\STF_note\\08.时空融合神经网络代码\\EDCSTFN\\data_cia\\train\\2001_290_1017\\1')
# file_name.mkdir(exist_ok=True)
# img = rasterio.open(path)
# current_band = img.read(1)
# profile = img.profile
# profile.update(count=1)
# result = rasterio.open(file_name, 'w',**profile)
# result.write(current_band, 1)
# result.close()
for img_path in image_dirs:
for img in Path(img_path).glob('*.tif'):
img_info = rasterio.open(img)
profile = img_info.profile
profile.update(count=1)
for prefix in order.values():
if img.name.startswith(prefix):
for i in range(NUM_BAND):
current_band = img_info.read(i+1)
new_path = final_data_path + 'band'+ str(i+1)+'\\'+current_processing
Path(new_path).mkdir(exist_ok=True)
new_path = new_path + '\\'+img_path.name
Path(new_path).mkdir(exist_ok=True)
file_name = new_path+'\\'+prefix+'.tif'
result = rasterio.open(file_name, 'w', **profile)
result.write(current_band, 1)
result.close()
img_info.close()
波段分离 单波段数据制造
最新推荐文章于 2024-10-04 20:30:00 发布
该代码段主要利用Python的os、pathlib、collections和rasterio库,对特定目录下的多波段图像数据进行处理。它首先创建目标文件夹,然后按顺序读取landsat和modis的波段,将每个波段拆分成单独的文件,重命名为指定格式,并保存到对应目录。处理过程涉及图像读取、写入、文件路径管理和目录创建。
摘要由CSDN通过智能技术生成