Python拆分atlas图集

在某游戏美术论坛里捡到了些别人分享的Spine动画,发现是Spine3.6生成的,我用的是3.8,用Spine自带的纹理解包器解包出来的部分小图带有莫名其妙的绿色背景,导入软件后就很喜感:

开始还以为这是什么高级的加密手段,但检查了一边发现并不是所有小图都带有背景,也有一部分正常的,再对照atlas中的信息发现所有异常的图像的rotate字段都是true,怀疑是解包算法的问题,把之前的旧的用python解包atlas的代码翻出来重新折腾了一下对rotate为true的图像的处理,可算是切分出正常的小图了


 完整代码:

# -*- coding: utf-8 -*-
import os
import sys
import os.path
import shutil
from PIL import Image

fileName = input('输入要解析的文件名:')

if fileName.find('.png') != -1:
    fileName = fileName[:-4]

pngName = fileName + '.png'
atlasName = fileName + '.atlas'

print(pngName,atlasName)

big_image = Image.open(pngName)
atlas = open(atlasName, encoding="utf8");

#big_image.show()#调用系统看图器

curPath = os.getcwd()# 当前路径
aim_path = os.path.join(curPath, fileName)
print (aim_path)
if os.path.isdir(aim_path):
    shutil.rmtree(aim_path,True)#如果有该目录,删除
os.makedirs(aim_path)

#读取文件中与解包无关的前几行字符串
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();

while True:
    line1 = atlas.readline() # name
    if len(line1) == 0:
        break
    else:
        line2 = atlas.readline() # rotate
        line3 = atlas.readline() # xy
        line4 = atlas.readline() # size
        line5 = atlas.readline() # orig
        line6 = atlas.readline() # offset
        line7 = atlas.readline() # index

        print("文件名:"+line1,end="")
        print("是否旋转:"+line2,end="")
        print("坐标:"+line3,end="")
        print("大小:"+line4,end="")
        print("原点:"+line5,end="")
        print("阻挡:"+line6,end="")
        print("索引:"+line7,end="")
        
        name = line1.replace("\n","") + ".png";
        
        args = line4.split(":")[1].split(",");
        width = int(args[0])
        height= int(args[1])
            
        args = line3.split(":")[1].split(",");
        ltx = int(args[0])
        lty = int(args[1])
        
        if (line2=='  rotate: true\n'):
            rbx = ltx+height
            rby = lty+width
        else:
            rbx = ltx+width
            rby = lty+height
        
        print ("文件名:"+name+" 宽度:"+str(width)+" 高度:"+str(height)+" 起始横坐标:"+str(ltx)+" 起始纵坐标:"+str(lty)+" 结束横坐标:"+str(rbx)+" 结束纵坐标:"+str(rby)+"\n")
        if (line2=='  rotate: true\n'):
            result_image = Image.new("RGBA", (height,width), (0,0,0,0))
            rect_on_big = big_image.crop((ltx,lty,rbx,rby))
            result_image.paste(rect_on_big, (0,0,height,width))
        else:
            result_image = Image.new("RGBA", (width,height), (0,0,0,0))
            rect_on_big = big_image.crop((ltx,lty,rbx,rby))
            result_image.paste(rect_on_big, (0,0,width,height))
        
        name_t=name.replace("/", "_")#字符替换

        if (line2=='  rotate: true\n'):
            result_image = result_image.transpose(Image.ROTATE_270)
        result_image.save(aim_path+'/'+name_t)
atlas.close()
del big_image

猜你喜欢

转载自blog.csdn.net/qq_36917144/article/details/122027354