有些文件使用了不同的版本,比如这个文件的前几个字节是“SCT2”。
请问你知道应该怎么修改脚本,才能把这种情况的文件也转换成 PNG 吗?
非常感谢你的帮助!
传文件…我没有下这个游戏
你可以传到谷歌网盘, 这个stc2我大概搞懂了结构, 你可以上传更多的文件吗
from pathlib import Path
from io import BytesIO, BufferedReader
from lz4.block import decompress as decLz4Block
from PIL.Image import Image, frombuffer as imgfb
from texture2ddecoder import decode_astc as dastc
class Sct2ToPng:
Sign: bytes = b'SCT2'
SignLen: int = len(Sign)
@classmethod
def Cov(cls, stream: BytesIO|BufferedReader) -> Image:
if stream.read(cls.SignLen) != cls.Sign:
raise ValueError('Sign 错误')
dataLen = int.from_bytes(stream.read(4), 'little')
_ = int.from_bytes(stream.read(4), 'little') # crc32?
offset = int.from_bytes(stream.read(4), 'little')
texType = int.from_bytes(stream.read(4), 'little')
_ = int.from_bytes(stream.read(4), 'little') # 不知道
width = int.from_bytes(stream.read(2), 'little')
height = int.from_bytes(stream.read(2), 'little')
# 应该是剪切后的宽度和高度
width2 = int.from_bytes(stream.read(2), 'little')
height2 = int.from_bytes(stream.read(2), 'little')
stream.seek(offset)
decLen = int.from_bytes(stream.read(4), 'little')
comLen = int.from_bytes(stream.read(4), 'little')
if comLen == dataLen - 80:
data = decLz4Block(stream.read(comLen), uncompressed_size=decLen)
else:
stream.seek(offset)
data = stream.read(comLen)
if texType == 4:
imgb = dastc(data, width, height, 4, 4)
return imgfb('RGBA', (width, height), imgb, 'raw', ('BGRA'))
else:
raise ValueError('贴图格式 错误')
@classmethod
def Read(cls, file: bytes|bytearray|str|Path|BytesIO|BufferedReader) -> Image:
if isinstance(file, str|Path):
with open(file, 'rb') as f:
return cls.Cov(f)
elif isinstance(file, (bytes, bytearray)):
return cls.Cov(BytesIO(file))
else:
return cls.Cov(file)
def batch(datPath: str = '', subfolder: bool = False):
path = Path(datPath) if datPath else Path.cwd()
need = [i for i in (path.rglob('*.sct') if subfolder else path.glob('*.sct')) if i.is_file()]
for i in need:
try:
img = Sct2ToPng.Read(i)
except Exception as e:
print(f'转换错误 --- {i.as_posix()} --- {e}')
continue
img.save(f'{i.as_posix()[:-3]}png')
if __name__ == '__main__':
path = r'' # sct文件所在的路径
subfolder = False # 是否查找字文件夹内的文件
batch(path, subfolder)
当然可以!这些是我在新补丁中一眼能找到的全部文件。
我刚刚测试了一下脚本,运行得非常完美,太感谢你了!
嗯, 都是astc4x4格式, 等你遇到转换出错的图片再和我说吧
1 个赞