import os import numpy as np from PIL import Image from osgeo import gdal # 输入和输出文件夹 src_dir = r"E:\datasets\WLKdata_1111\WLKdataset\images_LS" dst_dir = r"E:\datasets\WLKdata_1111\WLKdataset\images_LS_jpg" os.makedirs(dst_dir, exist_ok=True) for file in os.listdir(src_dir): if file.lower().endswith('.tif'): tif_path = os.path.join(src_dir, file) dataset = gdal.Open(tif_path) if dataset is None: print(f"无法打开: {tif_path}") continue # 读取所有波段 bands = [] for i in range(1, dataset.RasterCount + 1): band = dataset.GetRasterBand(i).ReadAsArray() bands.append(band) img = np.stack(bands, axis=-1) if len(bands) > 1 else bands[0] # 转换为uint8 img = img.astype(np.uint8) jpg_path = os.path.join(dst_dir, os.path.splitext(file)[0] + ".jpg") Image.fromarray(img).save(jpg_path, quality=95) print(f"已保存: {jpg_path}") print("全部转换完成!")