• 【python】plist图集拆分


    1.使用cocos creator的可以到官方商店找对应的插件工具,不过不是免费的... :( ,链接地址:cocos creator合图拆分

    2.使用cocos studio就简单一些,直接新建工程导入plist文件,进入文件所在文件夹,看到一个隐藏文件夹,进入里面就是想要的东西了。参考链接:cocos studio合图拆分

    3.就是使用python的PIL图片处理模块来拆分了:

    (1)安装python,这个自行百度吧,方法简单。

    (2)安装python的PIL模块:

    pip install Pillow

    (3)代码部分:

              split.py

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

    (4)将需要拆分的plist文件和图片跟脚本放在同一目录下

     (5)运行拆分脚本:

    python split.py block

    即可在当前目录下生成一个文件夹,里面包含了所有plist里的图片

     

  • 相关阅读:
    MODBUS转PROFINET网关将电力智能监控仪表接入PROFINET网络案例
    【苍穹外卖 | 项目日记】第四天
    whistle 的使用
    Hive常用操作持续更新!!!
    unity局部坐标和世界坐标角度介绍
    ros2移植Apollo和autoware规控算法可跑工程
    dbeaver导入excel数据
    Linux中的.bashrc文件
    WPF ToggleButton 主题切换动画按钮
    HCIP第十八天笔记
  • 原文地址:https://blog.csdn.net/sirria1/article/details/127772251