我正在尝试创建一个代码,允许我为不同文件夹中包含的不同图像堆叠同一图像的 10 个波段。我正在尝试创建一个代码,允许我为不同文件夹中包含的不同图像堆叠同一图像的 10 个波段。我在论坛上找到了一段代码,我试图从中获得灵感,但我无法将其应用于我的案例......有问题的代码如下:
from osgeo import gdal, gdal_array
import numpy as np
b1 = gdal.Open("LT50250232011160PAC01_sr_band1.tif")
b2 = gdal.Open("LT50250232011160PAC01_sr_band2.tif")
array1 = b1.ReadAsArray()
array2 = b2.ReadAsArray()
stacked = np.array([array1,array2])
gdal_array.SaveArray(stacked.astype("int"), "b12_stacked.tif", "GTiff", gdal.Open("LT50250232011160PAC01_sr_band1.tif"))
我的代码是:
import os
from osgeo import gdal, gdal_array
import numpy as np
src_directory = "/d/afavro/Bureau/test_subset/stack_resample_subset/"
dossier = os.listdir (src_directory)
print(dossier)
for fichier in dossier:
print (fichier)
ssrc_directory = "/d/afavro/Bureau/test_subset/stack_resample_subset/" + fichier + "/"
rasters = os.listdir (ssrc_directory)
print(rasters)
for img in rasters:
print (img)
INPUT_FOLDER = ssrc_directory
print(INPUT_FOLDER)
OUTPUT_FOLDER = "/d/afavro/Bureau/test_stack/" + fichier
print(OUTPUT_FOLDER)
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
# Define output file name
if img[-4:-3] == '.' :
file_img = OUTPUT_FOLDER+'/'+os.path.basename(img[:-4])
else:
file_img = OUTPUT_FOLDER+'/'+os.path.basename(img)
# Stack
band = gdal.Open(img)
array = band.ReadAsArray()
stacked = np.array(array)
gdal_array.SaveArray(stacked.astype("int"),"stack_" + fichier, "GTiff", prototype = None )
问题就在这里,我有错误消息:
band = gdal.Open(img)
^
IndentationError: unindent does not match any outer indentation level
我认为我们需要创建一个空矩阵或类似的东西,但我真的不明白怎么做,你有什么想法吗?