生成一段使用gdal批量将tif格式转换成png格式的带有注释的python代码
时间: 2024-04-29 18:25:18 浏览: 206
gdal裁剪tif_tif遥感图像_GDAL裁剪_遥感影像_GDAL_gdal批量裁剪_
5星 · 资源好评率100%
Sure, here's a Python code snippet that uses the GDAL library to batch convert TIFF files to PNG format:
```python
import os
from osgeo import gdal
# Set input and output directories
input_dir = '/path/to/tif/files'
output_dir = '/path/to/png/files'
# Loop through each file in input directory
for filename in os.listdir(input_dir):
if filename.endswith('.tif'):
# Open TIFF file
tif_file = gdal.Open(os.path.join(input_dir, filename))
# Set output file name
png_file = os.path.splitext(filename)[0] + '.png'
output_path = os.path.join(output_dir, png_file)
# Convert TIFF to PNG
gdal.Translate(output_path, tif_file, format='PNG')
# Add comment to output file
with open(output_path, 'a') as f:
f.write('# Converted from TIFF using GDAL')
print('Conversion complete')
```
Hope this helps! Let me know if you have any other questions.
阅读全文