分割Plist工具

分割plist文件提取资源。

很想项目Cocos2dx,CocosCreator都是用的plist文件。我们很多时候都想需要用到分割。

这里提供一个python写的工具。

源码如下:

#!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 == 'integer':
				d[item.text] = int(tree[index + 1].text) 
            elif tree[index+1].tag == 'dict':
                d[item.text] = tree_to_dict(tree[index+1])

    return d 
	
def read_rect(dict):
	return [dict['x'],dict['y'],dict['width'],dict['height']];
	
    
def gen_png_from_plist(plist_filename, png_filename):
    file_path = plist_filename.replace('.plist', '')
    big_image = Image.open(png_filename)
    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():
        if v.has_key('textureRect'):
            rectlist = to_list(v['textureRect'])
        elif v.has_key('frame'):
            rectlist = to_list(v['frame'])
        else:
			rectlist = read_rect(v)
			
        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,
            )
        #print box
        #print v
        if v.has_key('spriteSize'):
            spriteSize = v['spriteSize']
        elif v.has_key('sourceSize'):
            spriteSize = v['sourceSize']
        elif v.has_key("width"):
            spriteSize = str(v['width']) + ',' +  str(v['height'])

            
        sizelist = [ int(x) for x in to_list(spriteSize)]
        #print sizelist
        rect_on_big = big_image.crop(box)

        if (v.has_key('textureRotated') and v['textureRotated']) or (v.has_key('rotated') and v['rotated']):
            rect_on_big = rect_on_big.rotate(90)

        result_image = Image.new('RGBA', sizelist, (0,0,0,0))
        
        if (v.has_key('textureRotated') and v['textureRotated']) or (v.has_key('rotated') and v['rotated']):
            result_box=(
                ( sizelist[0] - height )/2,
                ( sizelist[1] - width )/2,
                ( sizelist[0] + height )/2,
                ( sizelist[1] + width )/2
                )
        else:
            result_box=(
                ( sizelist[0] - width )/2,
                ( sizelist[1] - height )/2,
                ( sizelist[0] + width )/2,
                ( sizelist[1] + height )/2
                )
        result_image.paste(rect_on_big, result_box, mask=0)

        if not os.path.isdir(file_path):
            os.mkdir(file_path)
        k = k.replace('/', '_')
        outfile = (file_path+'/' + k).replace('gift_', '')
        #print k
        if outfile.find('.png') == -1:
            outfile = outfile + '.png'
        print outfile, "generated"
        result_image.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 both plist and png files in the same directory"

想直接用的直接copy代码,保存文件unpackPlist.py即可。

把要分割的文件 放到unpackPlist.py 同级目录。(注意是.plist和.png配套).

 执行python  unpackPlist.py  分割的文件名 分割文件就会生成文件夹。

懒人直接下载内含 例子和第三方python库

链接:https://pan.baidu.com/s/16mw09vwE8_d62xNC693VBg 
提取码:qdol 


 

你可能感兴趣的:(Python)