python 拆分plist png

原地址: https://www.cnblogs.com/liujiang04/p/9771760.html

python 拆分plist png

python 需要安裝 PIL庫

http://www.pythonware.com/products/pil/

對應(yīng)版本

// mac 安裝 PIL庫
sudo pip install Pillow

#!python   
import os,sys  
from xml.etree import ElementTree  
from PIL import Image  
  
def endWith(s,*endstring):   
    array = map(s.endswith,endstring)   
    if True in array:   
        return True   
    else:   
        return False   
      
# Get the all files & directories in the specified directory (path).   
def get_recursive_file_list(path):   
    current_files = os.listdir(path)   
    all_files = []   
    for file_name in current_files:   
        full_file_name = os.path.join(path, file_name)  
        if endWith(full_file_name,'.plist'):   
            full_file_name = full_file_name.replace('.plist','')  
            all_files.append(full_file_name)   
   
        if os.path.isdir(full_file_name):   
            next_level_files = get_recursive_file_list(full_file_name)   
            all_files.extend(next_level_files)  
    return all_files  
      
def tree_to_dict(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] = tree_to_dict(tree[index+1])  
    return d  
  
def gen_png_from_plist(plist_filename, png_filename):  
    file_path = plist_filename.replace('.plist', '')  
    big_image = Image.open(png_filename)  
    root = ElementTree.fromstring(open(plist_filename, 'r').read())  
    plist_dict = tree_to_dict(root[0])  
    to_list = lambda x: x.replace('{','').replace('}','').split(',')  
    for k,v in plist_dict['frames'].items():  
        rectlist = to_list(v['frame'])  
        width = int( rectlist[3] if v['rotated'] else rectlist[2] )  
        height = int( rectlist[2] if v['rotated'] else rectlist[3] )  
        box=(   
            int(rectlist[0]),  
            int(rectlist[1]),  
            int(rectlist[0]) + width,  
            int(rectlist[1]) + height,  
        )  
        sizelist = [ int(x) for x in to_list(v['sourceSize']) ]  
        rect_on_big = big_image.crop(box)  
  
        if v['rotated']:  
            rect_on_big = rect_on_big.rotate(90)  
  
        result_image = Image.new('RGBA', sizelist, (0,0,0,0))  
        if v['rotated']:  
            result_box=(  
                ( sizelist[0] - height )/2,  
                ( sizelist[1] - width )/2,  
                ( sizelist[0] + height )/2,  
                ( sizelist[1] + width )/2  
            )  
        else:  
            result_box=(  
                ( sizelist[0] - width )/2,  
                ( sizelist[1] - height )/2,  
                ( sizelist[0] + width )/2,  
                ( sizelist[1] + height )/2  
            )  
        result_image.paste(rect_on_big, result_box, mask=0)  
  
        if not os.path.isdir(file_path):  
            os.mkdir(file_path)  
        outfile = (file_path+'/' + k).replace('gift_', '')  
        #outfile = outfile + '.png'  
        print outfile, "generated"  
        result_image.save(outfile)  
  
if __name__ == '__main__':  
    currtenPath = os.getcwd()   
    allPlistArray = get_recursive_file_list(currtenPath)  
    for plist in allPlistArray:  
        filename = plist  
        plist_filename = filename + '.plist'  
        png_filename = filename + '.png'  
        if (os.path.exists(plist_filename) and os.path.exists(png_filename)):  
            gen_png_from_plist( plist_filename, png_filename )  
        else:  
            print "make sure you have boith plist and png files in the same directory"
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市辜王,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖冯吓,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件睬辐,死亡現(xiàn)場離奇詭異,居然都是意外死亡拴签,警方通過查閱死者的電腦和手機(jī)纹份,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門苟跪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蔓涧,你說我怎么就攤上這事件已。” “怎么了元暴?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵篷扩,是天一觀的道長。 經(jīng)常有香客問我茉盏,道長鉴未,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任鸠姨,我火速辦了婚禮铜秆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘讶迁。我一直安慰自己连茧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布添瓷。 她就那樣靜靜地躺著,像睡著了一般值纱。 火紅的嫁衣襯著肌膚如雪鳞贷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天虐唠,我揣著相機(jī)與錄音搀愧,去河邊找鬼隔缀。 笑死,一個(gè)胖子當(dāng)著我的面吹牛劫拢,可吹牛的內(nèi)容都是我干的嫌佑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼迅箩,長吁一口氣:“原來是場噩夢啊……” “哼溉愁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起饲趋,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤拐揭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后奕塑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體堂污,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年龄砰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了盟猖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡换棚,死狀恐怖式镐,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情圃泡,我是刑警寧澤碟案,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站颇蜡,受9級特大地震影響价说,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜风秤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一鳖目、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缤弦,春花似錦领迈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至累提,卻和暖如春尘喝,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背斋陪。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工朽褪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留置吓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓缔赠,卻偏偏與公主長得像衍锚,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子嗤堰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    小邁克閱讀 2,989評論 1 3
  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007閱讀 2,210評論 0 3
  • 1-------- 走進(jìn)前端 2-------- jQuery 3-------- CSS 4-------- A...
    依依玖玥閱讀 2,324評論 0 34
  • 是否戴质,冥冥之中,我們早已擁有奇妙的緣梁棠? 是否置森,久遠(yuǎn)曾經(jīng),我們彼此早已美好約定符糊? 不然為何凫海,我們一到九華山,見到親愛...
    陽光Sunflower閱讀 4,296評論 12 13
  • Redis 入門 一.Redis簡介 Redis(http://redis.io)是一款開源的男娄、高性能的鍵-值存儲(chǔ)...
    hz_mumu閱讀 892評論 2 51