26 lines
886 B
Python
26 lines
886 B
Python
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 单通道灰度图。")
|