本文目录导读:

GDAL 命令行工具
基础批量转换
# 批量转换所有.tif文件为其他格式
for file in *.tif; do
gdal_translate -of JPEG "$file" "${file%.tif}.jpg"
done
常用格式转换
# TIF -> PNG
for f in *.tif; do
gdal_translate -of PNG "$f" "${f%.tif}.png"
done
# TIF -> JPEG(需指定压缩)
for f in *.tif; do
gdal_translate -of JPEG -co QUALITY=90 "$f" "${f%.tif}.jpg"
done
# 批量转换为ENVI格式
for f in *.tif; do
gdal_translate -of ENVI "$f" "${f%.tif}.dat"
done
Python + GDAL 脚本
import gdal
import os
import glob
def batch_convert(input_dir, output_dir, output_format='JPEG'):
"""
批量转换遥感影像格式
"""
# 创建输出目录
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 支持的文件格式映射
format_ext = {
'JPEG': '.jpg',
'PNG': '.png',
'GTiff': '.tif',
'ENVI': '.dat',
'HFA': '.img'
}
# 遍历所有tif文件
for tif_file in glob.glob(os.path.join(input_dir, '*.tif')):
try:
# 打开源文件
src_ds = gdal.Open(tif_file)
# 生成输出文件名
base_name = os.path.basename(tif_file).replace('.tif', '')
output_file = os.path.join(output_dir,
base_name + format_ext[output_format])
# 格式转换
driver = gdal.GetDriverByName(output_format)
dst_ds = driver.CreateCopy(output_file, src_ds, 0)
# 关闭数据集
dst_ds = None
src_ds = None
print(f"Converted: {tif_file} -> {output_file}")
except Exception as e:
print(f"Error converting {tif_file}: {str(e)}")
# 使用示例
batch_convert('./input_tifs/', './output_jpgs/', 'JPEG')
Rasterio 批量转换
import rasterio
from rasterio.shutil import copy
import glob
import os
def convert_raster_format(input_dir, output_dir, output_format='JPEG'):
"""使用rasterio批量转换"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
format_mapping = {
'JPEG': '.jpg',
'PNG': '.png',
'GTiff': '.tif',
'JP2OpenJPEG': '.jp2'
}
for input_path in glob.glob(os.path.join(input_dir, '*.tif')):
base = os.path.basename(input_path)
output_path = os.path.join(output_dir,
base.replace('.tif', format_mapping[output_format]))
try:
with rasterio.open(input_path) as src:
# 复制并转换格式
copy(src, output_path, driver=output_format)
print(f"Converted: {input_path} -> {output_path}")
except Exception as e:
print(f"Error: {e}")
# 使用示例
convert_raster_format('./input/', './output/', 'PNG')
批量转换并重采样
import gdal
import os
def batch_convert_resample(input_dir, output_dir, output_format='JPEG',
target_resolution=None):
"""批量转换并重采样"""
gdal.UseExceptions()
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 格式后缀映射
ext_map = {
'JPEG': '.jpg',
'PNG': '.png',
'GTiff': '.tif'
}
for file in os.listdir(input_dir):
if file.endswith(('.tif', '.img', '.dat')):
input_path = os.path.join(input_dir, file)
output_name = os.path.splitext(file)[0] + ext_map[output_format]
output_path = os.path.join(output_dir, output_name)
# 转换参数
options = gdal.TranslateOptions(
format=output_format,
xRes=target_resolution[0] if target_resolution else None,
yRes=target_resolution[1] if target_resolution else None,
resampleAlg='bilinear' # 重采样方法
)
# 执行转换
gdal.Translate(output_path, input_path, options=options)
print(f"Converted: {file} -> {output_name}")
# 使用示例
batch_convert_resample('./input/', './output/', 'JPEG', (30, 30))
并行批量转换(提高效率)
import gdal
import os
from concurrent.futures import ThreadPoolExecutor
import glob
def convert_single_file(input_file, output_dir, output_format):
"""单个文件转换函数"""
try:
base_name = os.path.basename(input_file)
output_name = base_name.replace('.tif', format_ext[output_format])
output_path = os.path.join(output_dir, output_name)
src_ds = gdal.Open(input_file)
driver = gdal.GetDriverByName(output_format)
dst_ds = driver.CreateCopy(output_path, src_ds, 0)
dst_ds = None
src_ds = None
return f"Success: {input_file}"
except Exception as e:
return f"Failed: {input_file} - {str(e)}"
def parallel_batch_convert(input_dir, output_dir, output_format='JPEG',
max_workers=4):
"""并行批量转换"""
format_ext = {
'JPEG': '.jpg',
'PNG': '.png',
'GTiff': '.tif'
}
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 获取所有输入文件
input_files = glob.glob(os.path.join(input_dir, '*.tif'))
# 使用线程池并行处理
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = []
for input_file in input_files:
future = executor.submit(convert_single_file,
input_file, output_dir, output_format)
futures.append(future)
# 获取结果
for future in futures:
print(future.result())
# 使用示例
parallel_batch_convert('./input/', './output/', 'PNG', max_workers=4)
批处理脚本(Windows)
@echo off
setlocal enabledelayedexpansion
:: 批量转换TIF为JPG
for %%f in (*.tif) do (
gdal_translate -of JPEG "%%f" "%%~nf.jpg"
echo Converted: %%f
)
:: 批量转换TIF为PNG(带重采样)
for %%f in (*.tif) do (
gdal_translate -of PNG -outsize 1000 0 "%%f" "%%~nf.png"
)
pause
注意事项:
- 格式支持:确保GDAL编译时支持所需的格式
- 内存管理:处理大文件时注意内存使用
- 颜色深度:不同格式支持的颜色深度可能不同
- 元数据:某些格式可能丢失部分元数据
- 压缩选项:根据需要设置压缩参数
选择哪种方法取决于你的具体需求、文件数量和系统环境,对于大量文件,建议使用并行处理版本以提高效率。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播