semantic-segmentation/datasets_pro_code/png_mask_trans.py
weixin_46229132 8148a58584 修bug
2025-05-30 09:37:22 +08:00

24 lines
682 B
Python

import os
import numpy as np
from PIL import Image
from osgeo import gdal
src_dir = r"E:\RSdata\wlk_right_448\mask_png_ori"
dst_dir = r"E:\RSdata\wlk_right_448\mask_png"
os.makedirs(dst_dir, exist_ok=True)
for file in os.listdir(src_dir):
if file.lower().endswith('.png'):
img = Image.open(os.path.join(src_dir, file))
mask = np.array(img)
# 替换像素值
mask = mask.copy()
mask[mask == 15] = 255
png_path = os.path.join(dst_dir, os.path.splitext(file)[0].replace("mask", "img") + ".png")
Image.fromarray(mask.astype(np.uint8)).save(png_path)
print(f"已保存: {png_path}")
print("全部转换完成!")