import os
from PIL import Image
folder_path = “.”
output_folder = “Converted”
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
# 检查文件是否是PNG图像
if filename.lower().endswith(“.png”):
# 打开图像并将通道数转换为RGBA
image = Image.open(file_path)
rgb_image = image.convert(“RGBA”)
width, height = rgb_image.size
print(width, height)
# 构造保存路径和文件名
save_path2 = os.path.join(output_folder, “DVR.rgba”)
raw_bytes = bytearray()
for r, g, b , a in rgb_image.getdata():
raw_bytes.extend([r, g, b, a])
with open(save_path2, ‘wb’) as f:
# 将字节数组写入文件
f.write(raw_bytes)
print(f"{filename} 的通道数已转换为RGB三通道,保存为 {save_path}")