You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需修改gdal_retile.py:按指定尺寸重叠切割批量影像并分目录存储

Hey there! Since you already know gdal_retile.py works for single large images, we don't need to reinvent the wheel—we can either wrap it in a simple batch script (easier for limited coding experience) or tweak the original script directly. Let's go through both approaches:

Approach 1: Batch Wrapper Script (No Changes to gdal_retile.py)

This is the simplest way—we'll write a tiny Python script to loop through all your images, create a subfolder for each, and call gdal_retile.py on each image pointing to its dedicated subfolder.

Here's the code you can use:

import os
import subprocess

# --------------------------
# Configure these parameters
# --------------------------
INPUT_FOLDER = "/path/to/your/large_images_folder"
OUTPUT_ROOT = "/path/to/where_you_want_tiles_saved"  # Can be same as INPUT_FOLDER
TILE_WIDTH = 512       # Your specified X tile size
TILE_HEIGHT = 512      # Your specified Y tile size
OVERLAP_PIXELS = 64    # Adjust overlap as needed (set to 0 if no overlap)
GDAL_RETILE_PATH = "gdal_retile.py"  # If it's in your PATH; otherwise use full file path

# Make sure output root exists
os.makedirs(OUTPUT_ROOT, exist_ok=True)

# Loop through all image files in input folder (add/remove extensions as needed)
for filename in os.listdir(INPUT_FOLDER):
    if filename.lower().endswith(('.tif', '.tiff', '.img', '.jp2', '.png')):
        # Get the image name without file extension
        image_name = os.path.splitext(filename)[0]
        # Create a subfolder named after the image
        tile_subfolder = os.path.join(OUTPUT_ROOT, image_name)
        os.makedirs(tile_subfolder, exist_ok=True)
        
        # Build the gdal_retile command
        full_input_path = os.path.join(INPUT_FOLDER, filename)
        retile_command = [
            "python", GDAL_RETILE_PATH,
            "-ps", str(TILE_WIDTH), str(TILE_HEIGHT),
            "-overlap", str(OVERLAP_PIXELS),
            "-targetDir", tile_subfolder,
            full_input_path
        ]
        
        # Run the command
        print(f"Starting processing for {filename}...")
        try:
            subprocess.run(retile_command, check=True)
            print(f"✅ Done! Tiles saved to {tile_subfolder}\n")
        except subprocess.CalledProcessError:
            print(f"❌ Failed to process {filename}\n")

How to use this:

  1. Update the configuration parameters at the top to match your paths and tile settings.
  2. Save this as batch_retile.py (or any name you like).
  3. Run it with Python: python batch_retile.py

This script will automatically handle each image, create its subfolder, and export tiles there—no need to modify the original gdal_retile.py at all.

Approach 2: Modify gdal_retile.py Directly

If you prefer to adjust the original script so it does this by default, here's what you can do:

  1. Open your gdal_retile.py file in a text editor.
  2. Find the section where it loops through input files (look for a loop over argv[1:] or similar—usually near the end of the script).
  3. Add code to create a subfolder for each input file and set the target directory to that subfolder.

First, make sure os is imported at the top of the script (it probably already is, but double-check):

import os

Then, find the loop that processes each input file. Look for code like this:

for input_file in argv[1:]:
    if not os.path.exists(input_file):
        print(f"File {input_file} does not exist. Skipping.", file=sys.stderr)
        continue
    # ... rest of the processing code ...

Modify that loop to add the subfolder creation:

# Get the original target directory set via command line
original_target_dir = options.targetDir

for input_file in argv[1:]:
    if not os.path.exists(input_file):
        print(f"File {input_file} does not exist. Skipping.", file=sys.stderr)
        continue
    
    # Create subfolder named after the input image (without extension)
    image_base_name = os.path.splitext(os.path.basename(input_file))[0]
    new_target_dir = os.path.join(original_target_dir, image_base_name)
    os.makedirs(new_target_dir, exist_ok=True)
    
    # Update the target directory for this image
    options.targetDir = new_target_dir
    
    # ... keep the rest of the original processing code here ...

# Optional: Reset target directory to original after loop if needed
options.targetDir = original_target_dir

How to use the modified script:

When you run gdal_retile.py, just specify your usual parameters plus the root output directory with -targetDir. It will automatically create subfolders for each input image inside that root directory.


Quick Tips:

  • Test with one small image first to make sure your tile size, overlap, and paths are correct.
  • If gdal_retile.py is in your system's PATH, you don't need to use the full file path in the wrapper script.
  • Remove the -overlap parameter from the command if you don't want overlapping tiles.

内容的提问来源于stack exchange,提问作者Alp Alt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:40:10