SMTTextureDecoder/decode.py

70 lines
2.3 KiB
Python
Raw Normal View History

2025-02-15 09:02:20 +08:00
import os
import numpy as np
import UnityPy
from PIL import Image
#made by Katboi01
def convert(in_path, out_path, file_name):
env = UnityPy.load(os.path.join(in_path, file_name))
2025-02-15 09:02:20 +08:00
textures = []
2025-02-15 09:02:20 +08:00
for obj in env.objects:
if obj.type.name == "Texture2D":
textures.append(obj)
2025-02-15 09:02:20 +08:00
if len(textures) > 1:
out_path = os.path.join(out_path, file_name)
os.makedirs(out_path, exist_ok=True)
2025-02-15 09:02:20 +08:00
for idx, obj in enumerate(textures):
try:
data = obj.read()
im : Image = data.image
width, height = im.size
2025-02-15 09:02:20 +08:00
left = int((width - 2) * 0.8)
2025-02-15 09:02:20 +08:00
im1 = im.crop((0, 0, left, height))
im1_array = np.asarray(im1, dtype=np.float32) / 255
y_channel = im1_array[:, :, 0]
alpha_channel = im1_array[:, :, 2]
2025-02-15 09:02:20 +08:00
im2 = im.crop((left+2, 0, width, height)).resize((left, height), Image.NEAREST)
im2_array = np.asarray(im2, dtype=np.float32) / 255
2025-02-15 09:02:20 +08:00
cr_channel = im2_array[:, :, 0]
cb_channel = im2_array[:, :, 2]
2025-02-15 09:02:20 +08:00
cr_channel -= 0.5
cb_channel -= 0.5
2025-02-15 09:02:20 +08:00
r_channel = np.clip(y_channel + (1.402 * cr_channel), 0, 1)
g_channel = np.clip(y_channel + (-0.344136 * cb_channel) + (-0.714136 * cr_channel), 0, 1)
b_channel = np.clip(y_channel + (1.772 * cb_channel), 0, 1)
2025-02-15 09:02:20 +08:00
new_image = np.stack([r_channel, g_channel, b_channel, alpha_channel], axis=-1)
new_image = (new_image * 255).astype(np.uint8)
result = Image.fromarray(new_image)
result.save(os.path.join(out_path, data.m_Name + ".png"), format="PNG")
except Exception as e:
print(f"Failed to extract ({idx}/{len(textures)}) from {file_name}", e)
2025-02-15 09:02:20 +08:00
def main():
in_path = "decoder_in"
out_path = "decoder_out"
if not os.path.isdir(in_path):
os.makedirs(in_path)
print("Put all files to extract in decoder_in folder")
input("Press Enter/Return to exit")
os.makedirs(out_path, exist_ok=True)
for _, _, files in os.walk(in_path):
for name in files:
print(name)
convert(in_path, out_path, name)
2025-02-15 09:02:20 +08:00
main()