Spine 动画拆出小图

Spine 动画拆出小图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PIL import Image
import os
import re
# 读取PNG图片和Atlas文件
image_file = 'D:/tools/pngTopng/avatar_hongbao.png'  # 请替换为您的PNG图片文件路径
atlas_file = 'D:/tools/pngTopng/avatar_hongbao.atlas'  # 请替换为您的Atlas文件路径
# 创建输出目录
output_dir = 'D:/tools/pngTopng/output_images/'
os.makedirs(output_dir, exist_ok=True)
# 读取Atlas文件内容
with open(atlas_file, 'r', encoding='utf-8') as f:
    atlas_data = f.read()
# 解析Atlas文件
matches = []
pattern = re.compile(r'id_(\w+)\n\s*rotate: (\w+)\n\s*xy: (\d+),(\d+)\n\s*size: (\d+),(\d+)')
matches = pattern.findall(atlas_data)
# 打开图片文件
image = Image.open(image_file)
# 遍历每个图块并裁剪出对应的小图
for match in matches:
    name, rotate, x, y, width, height = match
    x, y, width, height = int(x), int(y), int(width), int(height)
    if rotate == 'true':
        width, height = height, width
    # 从图片中裁剪出图块
    img_crop = image.crop((x, y, x + width, y + height))
   
    # 如果需要旋转图块,则旋转90度并更新尺寸
    if rotate == 'true':
        img_crop = img_crop.rotate(-90, expand=True)
   
    # 保存裁剪出的图块
    img_crop.save(os.path.join(output_dir, f"{name}.png"))
print(f"小图已经保存到目录: {output_dir}")