plist 切成小图

#!python
import os,sys
from xml.etree import ElementTree
from PIL import Image

def tree_to_dict(tree):
    d = {}
    for index, item in enumerate(tree):
        if item.tag == 'key':
            if tree[index+1].tag == 'string':
                d[item.text] = tree[index + 1].text
            elif tree[index + 1].tag == 'true':
                d[item.text] = True
            elif tree[index + 1].tag == 'false':
                d[item.text] = False
            elif tree[index+1].tag == 'dict':
                d[item.text] = tree_to_dict(tree[index+1])
    return d 
    
def gen_png_from_plist(plist_filename, png_filename):
    file_path = plist_filename.replace('.plist', '')
    big_image = Image.open(png_filename)
#xml解析
    root = ElementTree.fromstring(open(plist_filename, 'r').read())
    plist_dict = tree_to_dict(root[0])
    to_list = lambda x: x.replace('{','').replace('}','').split(',')
    for k,v in plist_dict['frames'].items():
        print k,v
        if v.has_key('textureRect'):
            rectlist = to_list(v['textureRect'])
        elif v.has_key('frame'):
            rectlist = to_list(v['frame'])
            print rectlist,'rectlist'
        if v.has_key('rotated'):
            width = int( rectlist[3] if v['rotated'] else rectlist[2] )
            height = int( rectlist[2] if v['rotated'] else rectlist[3] )
        else:
            width = int( rectlist[2] )
            height = int( rectlist[3] )
#不需要旋转时的剪裁框
        box = (
            int(rectlist[0]),
            int(rectlist[1]),
            int(rectlist[0]) + width,
            int(rectlist[1]) + height,
        )
#旋转90度的剪裁框 默认左上角为原点
        if (v.has_key('textureRotated') and v['textureRotated']) or (v.has_key('rotated') and v['rotated']):
            box = (
                int(rectlist[0]),
                int(rectlist[1]),
                int(rectlist[0]) + height,
                int(rectlist[1]) + width,
            )
        if v.has_key('spriteSize'):
            spriteSize = v['spriteSize']
        elif v.has_key('sourceSize'):
            spriteSize = v['sourceSize']
        sizelist = [ int(x) for x in to_list(spriteSize)]
        print sizelist,'sizelist'
        rect_on_big = big_image.crop(box)
        if (v.has_key('textureRotated') and v['textureRotated']) or (v.has_key('rotated') and v['rotated']):
#旋转图片 还有一种旋转方法rotate两种效果不全相同
            rect_on_big = rect_on_big.transpose(Image.ROTATE_90)
        if not os.path.isdir(file_path):
            os.mkdir(file_path)
        k = k.replace('/', '_')
        outfile = (file_path+'/' + k).replace('gift_', '')
        if outfile.find('.png') == -1:
            outfile = outfile + '.png'
        rect_on_big.save(outfile)
if __name__ == '__main__':
    filename = sys.argv[1]
    plist_filename = filename + '.plist'
    png_filename = filename + '.png'
    if (os.path.exists(plist_filename) and os.path.exists(png_filename)):
        gen_png_from_plist( plist_filename, png_filename )
    else:
        print "make sure you have boith plist and png files in the same directory"

Plist切成小图。方便易用。

需要工具 python2.7 

pil库。https://www.lfd.uci.edu/~gohlke/pythonlibs/

使用pil安装:《pip install + 文件路径》。  会自动识别信息比较方便。

以及入门知 识pil         https://www.cnblogs.com/kongzhagen/p/6295925.html

XML解析:http://www.runoob.com/python/python-xml.html

学习大佬的代码:https://blog.csdn.net/baidu20008/article/details/41445963

代码不是完全适用,发现对不同的plist有不同的解决方案。但是都是离不开文件解析和pil图片控制。

最主要的还是发生在对剪裁框的计算上面。

执行命令: python 脚本文件名.py 目标图集名字 

注意事项:脚本和图集在同一目录下。图集要包括 plist和png。

有问题的请留言一起学习。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值