MatchMaster/tools/extract-block-images.py
COMPUTER\EDY 4a352390b8 更新
2026-08-27 19:31:20 +08:00

56 lines
2.6 KiB
Python

"""从旧图集无损导出缺少的特殊方块小图;不会覆盖用户已有图片。需要 Pillow。"""
import json
import uuid
from pathlib import Path
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
def extract(atlas_path, frame):
with Image.open(atlas_path) as atlas:
x, y = frame["trimX"], frame["trimY"]
width, height = frame["width"], frame["height"]
rotated = frame["rotated"]
image = atlas.crop((x, y, x + (height if rotated else width), y + (width if rotated else height)))
if rotated:
image = image.transpose(Image.Transpose.ROTATE_90)
result = Image.new("RGBA", (frame["rawWidth"], frame["rawHeight"]))
left = round((result.width - width) / 2 + frame["offsetX"])
top = round((result.height - height) / 2 - frame["offsetY"])
result.paste(image, (left, top))
return result
def generate():
count = 0
for atlas_number in (6, 7):
atlas = ROOT / f"assets/BlockColor/block{atlas_number}.png"
meta = json.loads(atlas.with_suffix(".plist.meta").read_text(encoding="utf-8"))
for name, frame in meta["subMetas"].items():
# block6 的 10color 是问号外壳,不覆盖 Block/10color 普通颜色。
target_name = name.replace("10color", "question") if atlas_number == 6 else name
target = ROOT / "assets/Block" / target_name
if target.exists():
continue
image = extract(atlas, frame)
image.save(target)
texture_uuid = str(uuid.uuid5(uuid.NAMESPACE_URL, "MatchMaster/Block/" + target_name))
sprite_uuid = str(uuid.uuid5(uuid.NAMESPACE_URL, "MatchMaster/Block/" + target_name + "/spriteFrame"))
sprite = dict(frame, uuid=sprite_uuid, rawTextureUuid=texture_uuid, rotated=False,
trimType="none", offsetX=0, offsetY=0, trimX=0, trimY=0,
width=image.width, height=image.height)
sprite.pop("spriteType", None)
data = dict(ver="2.3.7", uuid=texture_uuid, importer="texture", type="sprite",
wrapMode="clamp", filterMode="bilinear", premultiplyAlpha=False,
genMipmaps=False, packable=False, width=image.width, height=image.height,
platformSettings={}, subMetas={target.stem: sprite})
target.with_suffix(".png.meta").write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
count += 1
print(f"Generated {count} special block images; existing images were not overwritten.")
if __name__ == "__main__":
generate()