还原TexturePacker plist 文件以及图片的方法 (切开各小图片)

还原TexturePacker plist 文件以及图片的方法 (切开各小图片)
分类: Code   5252人阅读  评论(11)  收藏  举报

Python 脚本:(来自网络)

unpack_plist.py

命令行: python unpack_plist.py  plist文件名称 

例子: python unpack_plist.py  common      ## plist文件全名为 common.plist

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #!python  
  2. import os,sys  
  3. from xml.etree import ElementTree  
  4. from PIL import Image  
  5.   
  6. def tree_to_dict(tree):  
  7.     d = {}  
  8.     for index, item in enumerate(tree):  
  9.         if item.tag == 'key':  
  10.             if tree[index+1].tag == 'string':  
  11.                 d[item.text] = tree[index + 1].text  
  12.             elif tree[index + 1].tag == 'true':  
  13.                 d[item.text] = True  
  14.             elif tree[index + 1].tag == 'false':  
  15.                 d[item.text] = False  
  16.             elif tree[index+1].tag == 'dict':  
  17.                 d[item.text] = tree_to_dict(tree[index+1])  
  18.     return d  
  19.   
  20. def gen_png_from_plist(plist_filename, png_filename):  
  21.     file_path = plist_filename.replace('.plist''')  
  22.     big_image = Image.open(png_filename)  
  23.     root = ElementTree.fromstring(open(plist_filename, 'r').read())  
  24.     plist_dict = tree_to_dict(root[0])  
  25.     to_list = lambda x: x.replace('{','').replace('}','').split(',')  
  26.     for k,v in plist_dict['frames'].items():  
  27.         rectlist = to_list(v['frame'])  
  28.         width = int( rectlist[3if v['rotated'else rectlist[2] )  
  29.         height = int( rectlist[2if v['rotated'else rectlist[3] )  
  30.         box=(   
  31.             int(rectlist[0]),  
  32.             int(rectlist[1]),  
  33.             int(rectlist[0]) + width,  
  34.             int(rectlist[1]) + height,  
  35.             )  
  36.         sizelist = [ int(x) for x in to_list(v['sourceSize'])]  
  37.         rect_on_big = big_image.crop(box)  
  38.   
  39.         if v['rotated']:  
  40.             rect_on_big = rect_on_big.rotate(90)  
  41.   
  42.         result_image = Image.new('RGBA', sizelist, (0,0,0,0))  
  43.         if v['rotated']:  
  44.             result_box=(  
  45.                 ( sizelist[0] - height )/2,  
  46.                 ( sizelist[1] - width )/2,  
  47.                 ( sizelist[0] + height )/2,  
  48.                 ( sizelist[1] + width )/2  
  49.                 )  
  50.         else:  
  51.             result_box=(  
  52.                 ( sizelist[0] - width )/2,  
  53.                 ( sizelist[1] - height )/2,  
  54.                 ( sizelist[0] + width )/2,  
  55.                 ( sizelist[1] + height )/2  
  56.                 )  
  57.         result_image.paste(rect_on_big, result_box, mask=0)  
  58.   
  59.         if not os.path.isdir(file_path):  
  60.             os.mkdir(file_path)  
  61.         outfile = (file_path+'/' + k).replace('gift_''')  
  62.         print outfile, "generated"  
  63.         result_image.save(outfile)  
  64.   
  65. if __name__ == '__main__':  
  66.     filename = sys.argv[1]  
  67.     plist_filename = filename + '.plist'  
  68.     png_filename = filename + '.png'  
  69.     if (os.path.exists(plist_filename) and os.path.exists(png_filename)):  
  70.         gen_png_from_plist( plist_filename, png_filename )  
  71.     else:  
  72.         print "make sure you have boith plist and png files in the same directory"  

windows7 下相关python配置:

1. 安装python2.7.3

2. 在此处下载 安装 (这是最简洁的方式,已经编译好png,zip等处理) 

  http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值