Files
Seg_Data_Server/Tool-可视化/Tool_Gen_8_Bit_PNG[没用,不认].py
2026-05-20 15:05:35 +08:00

26 lines
886 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from PIL import Image
import numpy as np
input_dir = 'Data/ann_dir' # 输入标签图所在目录
output_dir = 'Data/labels/train' # YOLO 格式输出目录
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if not filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp')):
continue
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, os.path.splitext(filename)[0] + '.png')
# 打开图像并转为 RGB再提取 R 通道(等价于 G、B
img_rgb = Image.open(input_path).convert('RGB')
r_channel = img_rgb.split()[0] # 或使用 .getchannel('R')
# 保存为 8-bit 单通道 PNG
r_channel.save(output_path, format='PNG')
print(f"Saved: {output_path}")
print("✅ 所有标签图已转换为 8-bit 单通道灰度图。")