Python ArcPy batch crops or cuts a large number of remote sensing images in multiple folders into quarters into N×M parts

  This article introduces a module based on Python ,ArcPy based on a large folder , traversing all remote sensing image raster files in each sub- folder, and cutting each original remote sensing image file into quarters , or cropping it into other specified number of small pieces .

  First, let’s clarify our needs. There is a large folder with multiple subfolders , as shown in the figure below.

  Among them, each sub-folder contains a large number of raster remote sensing image files (we will .tifintroduce the format of raster image files here as an example); for example, if you open any sub-folder in the above figure , you will see As shown in the figure below.

  What we hope to achieve is to crop and cut each remote sensing image in each sub-folder , and divide the original remote sensing image into new 4parts, which is equivalent to four equal parts . Here you can divide the original image into several parts if you want , that is, you can divide the original scene image into N * Mparts.

  Once we understand the requirements, we can start writing code. The code used in this article is actually the same as our previous article ArcPy in Python cuts raster images into multiple small parts in batches based on vector elements (https://blog.csdn.net/zhebushibiaoshifu/article/details/128481851), ArcPy in Python The code ideas mentioned in the method of dividing multiple raster images into multiple grid blocks (https://blog.csdn.net/zhebushibiaoshifu/article/details/129018876) are similar, but in file reading, There are differences in cropping parameter settings and other aspects; if you need it, you can check the above two articles first.

  The code used in this article is as follows.

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 28 22:39:52 2023

@author: fkxxgis
"""

import os
import arcpy

tif_file_path = r"E:/02_Project/202307_NDVIProduce/Beijing_Preprocessing/Original"
result_file_path = r"E:/02_Project/202307_NDVIProduce/Beijing_Preprocessing/Four_Result"
arcpy.env.parallelProcessingFactor = 0

for root, dirs, files in os.walk(tif_file_path):
    for dir_name in dirs:
        dir_path = os.path.join(root, dir_name)
        arcpy.env.workspace = dir_path
        tif_file_list = arcpy.ListRasters("*", "tif")

        for tif_file in tif_file_list:
            arcpy.SplitRaster_management(tif_file,
                                         result_file_path,
                                         tif_file.split(".tif")[0] + "_",
                                         "NUMBER_OF_TILES",
                                         "TIFF",
                                         "BILINEAR",
                                         "2 2",
                                         "#",
                                         "#",
                                         "PIXELS",
                                         "#",
                                         "#",
                                         "#",
                                         "#",
                                         "#",
                                         "0")
        print(dir_path)

  First, we need to set the input and output folder paths; where , tif_file_pathrepresents the large folder path that stores the original remote sensing images, and result_file_pathrepresents the folder path that stores the split results. Subsequently, you need to set ArcPythe environment parameters here. arcpy.env.parallelProcessingFactor = 0Disable parallel processing through this code to ensure that no problems will occur during processing. For the principle of this setting, please refer to the article ArcMap split raster Split Raster tool in ArcGIS. No results. The solution (https://fkxxgis.blog.csdn.net/article/details/128474804) is enough.

  Subsequently, we use to traverse all subfolders in os.walkthe source folder ; for each subfolder , set the working environment to the path of the subfolder , so that we can use Remote sensing image files.ArcPyarcpy.ListRasters.tif

  Next, for each remote sensing image, use arcpy.SplitRaster_managementa function to segment it. Among them, the first parameter tif_fileis the path of the remote sensing image to be split, the second parameter result_file_pathis the folder path where the split results are saved, and the third parameter tif_file.split(".tif")[0] + "_"is the prefix of the output file. Here, the original file name is used to remove .tifthe suffix, and Add an underscore at the end; the following parameters are used to set the splitting method, output format, interpolation method, etc. Here we choose to split the remote sensing image into 2 * 2blocks (if you need to split the remote sensing image into other quantities, you can modify it here), the interpolation method is BILINEAR, and the output format is TIFF. The last parameter 0indicates that the value in the original remote sensing image is used as the NoData0 value of the small image after cutting .

  By running the above code, we can see in the result folder that each remote sensing image has been divided into 4parts, and each file here has a numerical suffix (the numerical suffix is 0​​calculated from the beginning, this article Among them are 0, 1, 2and 3); as shown in the figure below.

  Next, if we want to copy or cut these small remote sensing images to other folders according to the characteristics of the file names, we can refer to the article Python to copy different files to the corresponding folders based on the file names of remote sensing images ( https://blog.csdn.net/zhebushibiaoshifu/article/details/129125339), Python traverses multiple subfolders and copies files to different target folders based on file name characteristics (https://blog.csdn.net/ The code mentioned in zhebushibiaoshifu/article/details/132332068) is automatically implemented.

  At this point, you're done.

Welcome to follow: Crazy Learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/132550536