Guest User

Untitled

a guest
Oct 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import os,sys
  4. from xml.etree import ElementTree
  5. from PIL import Image
  6. import logging
  7.  
  8. DEBUG = False
  9.  
  10. # 设置默认的level为DEBUG
  11. # 设置log的格式
  12. logging.basicConfig(
  13. level=logging.INFO if DEBUG else logging.ERROR,
  14. format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s"
  15. )
  16.  
  17.  
  18. def tree_to_dict(tree):
  19. d = {}
  20. for index, item in enumerate(tree):
  21. if item.tag == 'key':
  22. if tree[index+1].tag == 'string':
  23. d[item.text] = tree[index + 1].text
  24. elif tree[index + 1].tag == 'true':
  25. d[item.text] = True
  26. elif tree[index + 1].tag == 'false':
  27. d[item.text] = False
  28. elif tree[index+1].tag == 'dict':
  29. d[item.text] = tree_to_dict(tree[index+1])
  30. return d
  31.  
  32. def gen_png_from_plist(plist_filename, png_filename):
  33. file_path = plist_filename.replace('.plist', '')
  34. big_image = Image.open(png_filename)
  35. root = ElementTree.fromstring(open(plist_filename, 'r').read())
  36. plist_dict = tree_to_dict(root[0])
  37. to_list = lambda x: x.replace('{','').replace('}','').split(',')
  38. for k,v in plist_dict['frames'].items():
  39.  
  40. if(DEBUG and k != "coin_4_p.png"):
  41. continue
  42. logging.info(v)
  43.  
  44. rectlist = to_list(v['frame'])
  45. logging.info("rectlist: %s" % rectlist)
  46. width = int( rectlist[3] if v['rotated'] else rectlist[2] )
  47. height = int( rectlist[2] if v['rotated'] else rectlist[3] )
  48. logging.info("( %s , %s )" % (width, height))
  49. offset = [ abs(int(x)) for x in to_list(v['offset'])]
  50. logging.info("offset : %s -> %s " % (v['offset'], offset))
  51. sizelist = [ int(x) for x in to_list(v['sourceSize'])]
  52. logging.info("size: %s"% sizelist)
  53. logging.info("rotated: %s" % v['rotated'])
  54. box=(
  55. int(rectlist[0]),
  56. int(rectlist[1]),
  57. int(rectlist[0]) + width,
  58. int(rectlist[1]) + height,
  59. )
  60.  
  61. rect_on_big = big_image.crop(box)
  62.  
  63. logging.info(rect_on_big)
  64. if(DEBUG):
  65. result_image1 = Image.new('RGBA', [width, height], (0,0,0,0))
  66. result_image1.paste(rect_on_big, (0, 0, width, height))
  67.  
  68.  
  69. result_image = Image.new('RGBA', sizelist, (0,0,0,0))
  70. if v['rotated']:
  71. rect_on_big = rect_on_big.rotate(90,expand=1)
  72. parse_pos=(
  73. int((sizelist[0] - height)/2),
  74. int((sizelist[1] - width) /2)
  75. )
  76. else:
  77. parse_pos=(
  78. int((sizelist[1] - height)/2),
  79. int((sizelist[0] - width) /2)
  80. )
  81. logging.info(parse_pos)
  82. logging.info(rect_on_big)
  83. result_image.paste(rect_on_big, parse_pos)
  84.  
  85. if not os.path.isdir(file_path):
  86. os.mkdir(file_path)
  87. outfile = (file_path+'/' + k).replace('gift_', '')
  88. print(outfile, "generated")
  89. outpath = file_path +'/'+ outfile.split('/')[-1]
  90. print(outpath,"split")
  91. result_image.save(outpath)
  92. if(DEBUG):
  93. outpath1 = file_path +'/org_'+ outfile.split('/')[-1]
  94. print(outpath1,"split")
  95. result_image1.save(outpath1)
  96.  
  97.  
  98. if __name__ == '__main__':
  99. filename = sys.argv[1]
  100. plist_filename = filename + '.plist'
  101. png_filename = filename + '.png'
  102. if (os.path.exists(plist_filename) and os.path.exists(png_filename)):
  103. gen_png_from_plist( plist_filename, png_filename )
  104. else:
  105. print("make sure you have boith plist and png files in the same directory")
Add Comment
Please, Sign In to add comment