SoFunction
Updated on 2024-11-10

Python sample code to realize the combination of remote sensing image bands

Recently to do a remote sensing-related small system, the need for band combination function, online to find can use ArcGIS installation comes with the arcpy package, but Python3.7 can not use the existing ArcGIS10.2 version, and do not want to install the other version, so can only think of a way to solve. But it's a bit stupid.

The thinking is:

1. Reading requires combining remote sensing image bands (OLI in this case)

2. Create an array and put the read bands into it in sequence

3. Write files to tif multiband data

Up Code:

from osgeo import gdal
import os
import numpy as np
 
class GRID:
 
  #Read image files
  def read_img(self,filename):
    dataset=(filename)    # Open the file
 
    im_width =   # of columns in the raster matrix
    im_height =   # of rows of the raster matrix
 
    im_geotrans = () # affine matrix
    im_proj = () # Map projection information
    im_data = (0,0,im_width,im_height) # Write the data as an array, corresponding to the raster matrix
 
    del dataset #Close object, file dataset
    return im_proj,im_geotrans,im_data,im_width,im_height
 
  #write file to write to tif for example
  def write_img(self,filename,im_proj,im_geotrans,im_data):
 
    # Determine the data type of raster data
    if 'int8' in im_data.:
      datatype = gdal.GDT_Byte
    elif 'int16' in im_data.:
      datatype = gdal.GDT_UInt16
    else:
      datatype = gdal.GDT_Float32
 
    # Interpretation of array dimensions
    if len(im_data.shape) == 3:
      im_bands, im_height, im_width = im_data.shape
    else:
      im_bands, (im_height, im_width) = 1,im_data.shape
 
    #Creating files
    driver = ("GTiff")  # Data types must be available because to calculate how much memory space is required
    dataset = (filename, im_width, im_height, im_bands, datatype)
 
    (im_geotrans)       #Write affine transformation parameters
    (im_proj)          #Write projection
 
    if im_bands == 1:
      (1).WriteArray(im_data) # Write array data
    else:
      for i in range(im_bands):
        (i+1).WriteArray(im_data[i])
 
    del dataset
 
if __name__ == "__main__":
  (r'E:\Python\temp\data')            # Switch the path to the folder where the image to be processed is located
  run = GRID()
  # First step
  proj,geotrans,data1,row1,column1 = run.read_img('Band_5_Clip.tif') #Read the data
  proj,geotrans,data2,row2,column2= run.read_img('Band_4_Clip.tif') # Read the data
  proj,geotrans,data3,row3,column3 = run.read_img('Band_3_Clip.tif') # Read the data
 
  # Step two
  data = ((data1, data2, data3),dtype = ) # Put the 3 band image element values in order into the
 
  # Step three
  run.write_img('', proj, geotrans, data) # put down as3Band data

OK! A comparison with the ArcGIS processing shows little difference (top: ArcMap bottom: Python).

The method is rather stupid, if you gods have a better method, we can communicate with each other privately.

This is the whole content of this article.