cocos2dx 游戏plist与png完美切成小图python代码

 

  首先需要一份python的切图程序:

  1 #python2.5 unpack_plist.py birdfly 
  2 
  3 
  4 #! /usr/lical/bin/python
  5 import os,sys
  6 from xml.etree import ElementTree
  7 from PIL import Image 
  8 
  9 def tree_to_dict(tree):
 10     d = {}
 11     for index, item in enumerate(tree):
 12         if item.tag == 'key':
 13             if tree[index+1].tag == 'string':
 14                 d[item.text] = tree[index + 1].text
 15             elif tree[index + 1].tag == 'true':
 16                 d[item.text] = True
 17             elif tree[index + 1].tag == 'false':
 18                 d[item.text] = False
 19             elif tree[index+1].tag == 'dict':
 20                 d[item.text] = tree_to_dict(tree[index+1])
 21     return d
 22 
 23 def gen_png_from_plist(plist_filename, png_filename):
 24     file_path = plist_filename.replace('.plist', '')
 25     big_image = Image.open(png_filename)
 26     root = ElementTree.fromstring(open(plist_filename, 'r').read())
 27     plist_dict = tree_to_dict(root[0])
 28     to_list = lambda x: x.replace('{','').replace('}','').split(',')
 29     for k,v in plist_dict['frames'].items():
 30         print "-----start\n----------"
 31         rectlist = to_list(v['frame'])
 32         print rectlist, "--------rectlist"
 33         width = int( rectlist[3] if v['rotated'] else rectlist[2] )
 34         height = int( rectlist[2] if v['rotated'] else rectlist[3] )
 35         print width,height,"----width,height"
 36         box=( 
 37             int(rectlist[0]),
 38             int(rectlist[1]),
 39             int(rectlist[0]) + width,
 40             int(rectlist[1]) + height,
 41             )
 42         # bos is start & end point
 43         print box,"-----_box-"
 44         print v['rotated'], "---rotated"
 45 
 46         sizelist = [ int(x) for x in to_list(v['sourceSize'])]
 47         rect_on_big = big_image.crop(box)
 48         '''
 49         result_image = Image.new('RGBA', sizelist, (0,0,0,0))
 50         result_box=(
 51             ( sizelist[0] - width )/2,
 52             ( sizelist[1] - height )/2,
 53             ( sizelist[0] + width )/2,
 54             ( sizelist[1] + height )/2
 55             )
 56         result_image.paste(rect_on_big, result_box, mask=0)
 57         if v['rotated']:
 58             result_image = result_image.rotate(90)
 59         if not os.path.isdir(file_path):
 60             os.mkdir(file_path)
 61         outfile = (file_path+'/' + k).replace('gift_', '')
 62         print result_box,"-----result_box-"
 63         print outfile, "generated"
 64         # result_image.save(outfile)
 65         '''
 66 
 67         if v['rotated']:
 68             # rect_on_big = rect_on_big.rotate(90) 
 69             rect_on_big = rect_on_big.transpose(Image.ROTATE_90)
 70   
 71         # result_image = Image.new('RGBA', sizelist, (0,0,0,0))  
 72         # if v['rotated']:  
 73         #     result_box=(  
 74         #         ( sizelist[0] - height )/2,  
 75         #         ( sizelist[1] - width )/2,  
 76         #         ( sizelist[0] + height )/2,  
 77         #         ( sizelist[1] + width )/2  
 78         #         )  
 79         # else:  
 80         #     result_box=(  
 81         #         ( sizelist[0] - width )/2,  
 82         #         ( sizelist[1] - height )/2,  
 83         #         ( sizelist[0] + width )/2,  
 84         #         ( sizelist[1] + height )/2  
 85         #         )  
 86         # result_image.paste(rect_on_big, result_box, mask=0)      
 87 
 88         if not os.path.isdir(file_path):
 89             os.mkdir(file_path)
 90         outfile = (file_path+'/' + k).replace('gift_', '')
 91         rect_on_big.save(outfile);
 92 
 93 if __name__ == '__main__':
 94     filename = sys.argv[1]
 95     plist_filename = filename + '.plist'
 96     png_filename = filename + '.png'
 97 if (os.path.exists(plist_filename) and os.path.exists(png_filename)):
 98     gen_png_from_plist( plist_filename, png_filename )
 99 else:
100     print "make sure you have boith plist and png files in the same directory"

 

Mac 电脑下可以新建一个文本把程序复制进去,保存后缀名为 .py 并移至与需要切的plist与png文件夹下。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值