plist圖片列表分割拆分器(修復(fù)輸出圖片錯(cuò)誤的BUG)

網(wǎng)上的所有python版本代碼,對(duì)于新版本的plist都是錯(cuò)誤的!

本人做了修改,主要有:

1.更多旋轉(zhuǎn)標(biāo)簽增加支持;

2.python的圖片旋轉(zhuǎn)函數(shù)調(diào)用錯(cuò)誤,修改為transpose;

具體請(qǐng)復(fù)制以下代碼保存為py文件,例如up.py,然后在plist和圖片文件所在文件夾運(yùn)行:up.py -plist c:\1.plist -png c:\1.png -dir c:\1\

注意:

以上命令行“c:\1”文件夾提前創(chuàng)建才行。

以上命令,需要提前安裝好python環(huán)境,并安裝Pillow才行(自己去搜索吧);

看代碼:

#!/usr/bin/env?python??

#?coding=utf-8??

#?Python?2.7.10??


"""

????This?utility?is?used?to?parse?plist?file?which?is?packed?by?Texture?Packer?to?original?images.

????usage:

????????-plist?specify?the?path?of?plist?file(required?parameter).

????????-png?specify?the?path?of?png?file(required?parameter).

????????-dir?specify?a?output?directory(optional).?By?default,?it?will?make?a?new?directory?named

?????????????with?plist?filename?in?current?directory?to?save?images.

"""??



import?argparse??

import?os??

import?sys??

from?xml.etree?import?ElementTree??

from?PIL?import?Image??



class?PlistParser(object):??


#?initializer??

def?__init__(self,?plist,?png_image,?output_dir):??

self.plist_file?=?plist??

self.png_file?=?png_image??

self.atlas_dir?=?output_dir??


#?convert?a?xml?tree?to?dict.??

def?convert_tree_to_dict(self,?tree):??

????????d?=?{}??

for?index,?item?in?enumerate(tree):??

if?item.tag?==?'key':??

if?tree[index?+?1].tag?==?'string':??

d[item.text]?=?tree[index?+1].text??

elif?tree[index?+?1].tag?==?'true':??

d[item.text]?=True??

elif?tree[index?+?1].tag?==?'false':??

d[item.text]?=False??

elif?tree[index?+?1].tag?==?'dict':??

d[item.text]?=self.convert_tree_to_dict(tree[index?+?1])??

return?d??


#?split?png?file?into?individual?images.??

def?split_png_from_plist(self):??

#?generate?output?directory.??

target_file_dir?=self.atlas_dir;??

if?target_file_dir?is?None:??

target_file_dir?=?plist_filename.replace('.plist',?'')??

if?not?os.path.isdir(target_file_dir):??

????????????????os.mkdir(target_file_dir)??


#?open?the?source?image.??

????????src_image?=?Image.open(png_filename)??

plist_content?=?open(plist_filename,'r').read()??

????????plist_root?=?ElementTree.fromstring(plist_content)??

plist_dict?=self.convert_tree_to_dict(plist_root[0])??


to_list?=lambda?x?:?x.replace('{',?'').replace('}',?'').split(',')??

for?k,?v?in?plist_dict['frames'].items():??

pos_str?=?str(v['frame'])??

????????????rect_list?=?to_list(pos_str)??

width?=?int(rect_list[3]?if??(v.has_key('textureRotated')?and?v['textureRotated'])?or?(v.has_key('rotated')?and?v['rotated'])?else?rect_list[2]?)??

height?=?int(rect_list[2]?if??(v.has_key('textureRotated')?and?v['textureRotated'])?or?(v.has_key('rotated')?and?v['rotated'])?else?rect_list[3]?)??

????????????bounding_box?=?(??

int(rect_list[0]),??

int(rect_list[1]),??

int(rect_list[0])?+?width,??

int(rect_list[1])?+?height,??

????????????)??

#size_list?=?[?int(x)?for?x?in?to_list(v['sourceSize'])?]??


#?print?k,bounding_box??

????????????rect_image?=?src_image.crop(bounding_box)??

if??(v.has_key('textureRotated')?and?v['textureRotated'])?or?(v.has_key('rotated')?and?v['rotated']):??

????????????????rect_image?=?rect_image.transpose(Image.ROTATE_90)??


????????????outfile?=?os.path.join(target_file_dir,?k)??

????????????rect_image.save(outfile)??



if?__name__?==?'__main__':??

#?register?all?available?parameters.??

parser?=?argparse.ArgumentParser(usage='please?use?unpacker.py?-h?to?get?usage?information.')??

parser.add_argument('-plist',?help='Specify?the?path?of?plist?file.',?type=str)??

parser.add_argument('-png',?help='Specify?the?path?of?png?file.',?type=str)??

parser.add_argument('-dir',?help='Specify?a?output?directory.',?type=str)??


#?get?parameters.??

????args?=?parser.parse_args()??

????plist_filename?=?args.plist??

????png_filename?=?args.png??

????output_dir?=?args.dir??


if?plist_filename?is?None?or?png_filename?is?None?or?output_dir?is?None:??

print?"example:?python?unpacker.py?-plist?your_plist_file.plist?-png?your_png_file.png?-dir?output_directory"??

sys.exit(1);??


#?test?whether?the?file/dir?is?None??

if?plist_filename?is?None:??

print?'make?sure?to?use?-plist?to?specify?the?plist?file?path.'??

sys.exit(1)??

if?png_filename?is?None:??

print?'make?sure?to?use?-png?to?specify?the?source?png?image.'??

sys.exit(1)??


#?test?whether?the?file/dir?exits??

if?not?os.path.exists(plist_filename):??

print?'error:?plist?file?doesn\'t?exist.'??

sys.exit(1)??

if?not?os.path.exists(png_filename):??

print?'error:?png?file?doesn\'t?exist.'??

sys.exit(1)??

if?output_dir?is?not?None?and?not?os.path.isdir(output_dir):??

print?'error:?%s?is?no?an?valid?directory?or?doesn\'t?exist.'?%?output_dir??

sys.exit(1)??


????plist_parser?=?PlistParser(plist_filename,?png_filename,?output_dir)??

????plist_parser.split_png_from_plist()??

print?'OK!?For?more,qq-group:222670733'??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容