博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根据 plist 还原 图片
阅读量:6863 次
发布时间:2019-06-26

本文共 4111 字,大约阅读时间需要 13 分钟。

 

1. python 环境自己配置(支持windows Mac )

2. 把所有的 plist  和 大图片放到一个目录下

3.如果添加了 系统环境变量 就直接双击运行脚本,如果没有设置,把脚本拽到DOS窗口下运行 就OK了

4.   (支持 windows)

5. 必须安装库 

 

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

 

转载于:https://www.cnblogs.com/GameDeveloper/p/3755620.html

你可能感兴趣的文章
linux 命令篇 -- 新建用户
查看>>
基于python的大数据分析实战学习笔记-pandas(数据分析包)
查看>>
火狐访问HTTPS网站显示连接不安全的解决方法
查看>>
nGrinder中快速编写groovy脚本03-在GET请求中发送参数
查看>>
转盘抽奖
查看>>
Dubbo中基于权重的随机算法
查看>>
<问题解决10>使用带有框架的页面跳转到登录页面时,登录页面只显示在子框架中,未能铺满整个浏览器--解决方案如下:...
查看>>
oracle 锁表
查看>>
UML关系说明文档
查看>>
如何去除My97 DatePicker控件上右键弹出官网的链接 - 如何debug混淆过的代码
查看>>
dedecms织梦移站后替换数据库中文件路径命令
查看>>
openssl
查看>>
2017 WWDC
查看>>
iOS 动态更换icon
查看>>
dd命令测试IO
查看>>
Jmeter连接MySQL数据库
查看>>
HDU5446:Unknown Treasure——题解
查看>>
在C#中SendMessage和PostMessage的参数传递
查看>>
维护MMO项目的随想
查看>>
Resharper 检测所有NullReferenceExceptions(空指针)
查看>>