根据 plist 还原 图片
1. python 环境自己配置(支持windows Mac )
2. 把所有的 plist 和 大图片放到一个目录下
3.如果添加了 系统环境变量 就直接双击运行脚本,如果没有设置,把脚本拽到DOS窗口下运行 就OK了
4. http://download.csdn.net/detail/wanggan768q/7411533 (支持 windows)
5.http://download.csdn.net/detail/wanggan768q/7432769 必须安装库
1 #!python 2 import os,sys 3 from xml.etree import ElementTree 4 from PIL import Image 5 6 def endWith(s,*endstring): 7 array = map(s.endswith,endstring) 8 if True in array: 9 return True 10 else: 11 return False 12 13 # Get the all files & directories in the specified directory (path). 14 def get_recursive_file_list(path): 15 current_files = os.listdir(path) 16 all_files = [] 17 for file_name in current_files: 18 full_file_name = os.path.join(path, file_name) 19 if endWith(full_file_name,'.plist'): 20 full_file_name = full_file_name.replace('.plist','') 21 all_files.append(full_file_name) 22 23 if os.path.isdir(full_file_name): 24 next_level_files = get_recursive_file_list(full_file_name) 25 all_files.extend(next_level_files) 26 27 return all_files 28 29 30 def tree_to_dict(tree): 31 d = {} 32 for index, item in enumerate(tree): 33 if item.tag == 'key': 34 if tree[index+1].tag == 'string': 35 d[item.text] = tree[index + 1].text 36 elif tree[index + 1].tag == 'true': 37 d[item.text] = True 38 elif tree[index + 1].tag == 'false': 39 d[item.text] = False 40 elif tree[index+1].tag == 'dict': 41 d[item.text] = tree_to_dict(tree[index+1]) 42 return d 43 44 def gen_png_from_plist(plist_filename, png_filename): 45 file_path = plist_filename.replace('.plist', '') 46 big_image = Image.open(png_filename) 47 root = ElementTree.fromstring(open(plist_filename, 'r').read()) 48 plist_dict = tree_to_dict(root[0]) 49 to_list = lambda x: x.replace('{','').replace('}','').split(',') 50 for k,v in plist_dict['frames'].items(): 51 rectlist = to_list(v['frame']) 52 width = int( rectlist[3] if v['rotated'] else rectlist[2] ) 53 height = int( rectlist[2] if v['rotated'] else rectlist[3] ) 54 box=( 55 int(rectlist[0]), 56 int(rectlist[1]), 57 int(rectlist[0]) + width, 58 int(rectlist[1]) + height, 59 ) 60 sizelist = [ int(x) for x in to_list(v['sourceSize'])] 61 rect_on_big = big_image.crop(box) 62 63 if v['rotated']: 64 rect_on_big = rect_on_big.rotate(90) 65 66 result_image = Image.new('RGBA', sizelist, (0,0,0,0)) 67 if v['rotated']: 68 result_box=( 69 ( sizelist[0] - height )/2, 70 ( sizelist[1] - width )/2, 71 ( sizelist[0] + height )/2, 72 ( sizelist[1] + width )/2 73 ) 74 else: 75 result_box=( 76 ( sizelist[0] - width )/2, 77 ( sizelist[1] - height )/2, 78 ( sizelist[0] + width )/2, 79 ( sizelist[1] + height )/2 80 ) 81 result_image.paste(rect_on_big, result_box, mask=0) 82 83 if not os.path.isdir(file_path): 84 os.mkdir(file_path) 85 outfile = (file_path+'/' + k).replace('gift_', '') 86 print outfile, "generated" 87 result_image.save(outfile) 88 89 if __name__ == '__main__': 90 91 92 currtenPath = os.getcwd() 93 allPlistArray = get_recursive_file_list(currtenPath) 94 95 for plist in allPlistArray: 96 filename = plist 97 print filename 98 plist_filename = filename + '.plist' 99 png_filename = filename + '.png' 100 if (os.path.exists(plist_filename) and os.path.exists(png_filename)): 101 gen_png_from_plist( plist_filename, png_filename ) 102 else: 103 print "make sure you have boith plist and png files in the same directory" 104 105