30 lines
919 B
Python
30 lines
919 B
Python
import os
|
|
import numpy as np
|
|
from PIL import Image
|
|
from osgeo import gdal
|
|
|
|
src_dir = r"E:\datasets\WLKdata_1111\WLKdataset\masks_LS"
|
|
dst_dir = r"E:\datasets\WLKdata_1111\WLKdataset\masks_LS_png"
|
|
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
|
|
mask = dataset.ReadAsArray()
|
|
if mask.ndim != 2:
|
|
print(f"{file} 不是单波段,跳过")
|
|
continue
|
|
|
|
# 替换像素值
|
|
mask = mask.copy()
|
|
mask[mask == 15] = 255
|
|
|
|
png_path = os.path.join(dst_dir, os.path.splitext(file)[0] + ".png")
|
|
Image.fromarray(mask.astype(np.uint8)).save(png_path)
|
|
print(f"已保存: {png_path}")
|
|
|
|
print("全部转换完成!") |