最近在研究AE溶图,需要拆立绘,但是发现部分半透明材质发黑发灰
原因:
另外需要注意pna解包后的图片和普通的图片不一样,普通图片的透明像素是#FFFFFF00,pna里的图片透明像素是#00000000,改完之后需要转换一下(脚本地址,懒得自己写,有现成轮子为啥要自己写?),否则透明像素进游戏会变成白色 (来自Willplus引擎AdvHD汉化踩坑记录 – 存在感很低的Asanatsa)
解决方案(自动处理同目录下所有png)
import os
import sys
import numpy as np
from PIL import Image
def fix_pna_images(root_dir):
fixed_count = 0
for root, dirs, files in os.walk(root_dir):
for file in files:
if file.lower().endswith((".png", ".bmp")):
filepath = os.path.join(root, file)
try:
# 强制转换为 RGBA 模式
img = Image.open(filepath).convert("RGBA")
arr = np.array(img)
# 提取 Alpha 通道
alpha = arr[:, :, 3]
# --- 1. 处理纯透明区域 (Alpha=0) ---
# 将完全透明区域的 RGB 强制设为 0,消除杂色
arr[alpha == 0] = [0, 0, 0, 0]
# --- 2. 处理预乘 Alpha (去黑边/暗边) ---
# PNA 解包出的边缘如果是黑色/灰色,通常是因为 RGB 值被乘以了 Alpha。
# 将其还原为正常的颜色(防止边缘黑边)。
# 使用 numpy 批量除法还原预乘
mask = (alpha > 0) & (alpha < 255)
if np.any(mask):
# 将 alpha 归一化到 0-1 之间
alpha_norm = alpha[mask].astype(np.float32) / 255.0
# 还原 RGB
arr[mask, 0] = np.minimum(255, (arr[mask, 0].astype(np.float32) / alpha_norm)).astype(np.uint8)
arr[mask, 1] = np.minimum(255, (arr[mask, 1].astype(np.float32) / alpha_norm)).astype(np.uint8)
arr[mask, 2] = np.minimum(255, (arr[mask, 2].astype(np.float32) / alpha_norm)).astype(np.uint8)
# --- 3. 通道顺序修正 (防止红蓝颠倒) ---
# 如果提取出的图颜色全错了(比如人脸变蓝,衣服变红),
# 取消下面这行注释即可强制将 BGRA 转为 RGBA
# arr = arr[:, :, [2, 1, 0, 3]]
# 转换回图像并保存为高质量的标准 PNG
new_img = Image.fromarray(arr, "RGBA")
new_img.save(filepath, format="PNG")
print(f"已修复: {filepath}")
fixed_count += 1
except Exception as e:
print(f"处理文件 {filepath} 时出错: {e}")
print(f"完成!共修复了 {fixed_count} 张图片。")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python fix_pna_to_png.py <解包出的图片文件夹路径>")
else:
fix_pna_images(sys.argv[1])
处理前

处理后

最后陪一张AE溶的成图(手艺不精)



Comments NOTHING