如何从plist文件中提取png图像?

时间:2011-05-22 07:21:51

标签: png plist

我目前有一组plist文件。里面是png图像,有些文件有很多图像。我正在使用win64 vista。

我专门为此寻找过像FileJuicer这样的东西,但这只适用于mac用户。

5 个答案:

答案 0 :(得分:3)

我已编写此脚本以解压TexturePacker

打包的png文件中的plist个文件

首先,请确保在同一目录中同时包含png和plist文件:freeGifts.plist和freeGifts.png

其次,与unpack_plist.py

相同的以下脚本

然后,python unpack_plist.py freeGifts,它会生成大量的png文件到名为freeGifts的目录

要求:python,PIL

#! /usr/lical/bin/python
import os,Image,sys
from xml.etree import ElementTree

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)
        result_image = Image.new('RGBA', sizelist, (0,0,0,0))
        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 v['rotated']:
            result_image = result_image.rotate(90)
        if not os.path.isdir(file_path):
            os.mkdir(file_path)
        outfile = (file_path+'/' + k).replace('gift_', '')
        print outfile, "generated"
        result_image.save(outfile)

if __name__ == '__main__':
    filename = sys.argv[1]
    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"

答案 1 :(得分:0)

大笑,这就是史蒂夫乔布斯所做的一切。您可以使用ChadBurggraf的库https://github.com/ChadBurggraf/plists-cs在C#中解析plist。但正如评论中所讨论的,您想要的文件可能不在plist文件中。

答案 2 :(得分:0)

感谢Sean.Z.
对于unpack_plist.py,您需要:
1:PIL已安装在您的电脑上。
2:使用python 2.5
    python2.5 unpack_plist.py birdfly
或者你会失败 ----- 2013-07-25--
对于一些旋转的plist,上面的spcript可能有一些bug。我修改它如下:



#python2.5 unpack_plist.py birdfly 


#! /usr/lical/bin/python
import os,Image,sys
from xml.etree import ElementTree

    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():
        print "-----start\n----------"
        rectlist = to_list(v['frame'])
        print rectlist, "--------rectlist"
        width = int( rectlist[3] if v['rotated'] else rectlist[2] )
        height = int( rectlist[2] if v['rotated'] else rectlist[3] )
        print width,height,"----width,height"
        box=( 
            int(rectlist[0]),
            int(rectlist[1]),
            int(rectlist[0]) + width,
            int(rectlist[1]) + height,
            )
        # bos is start & end point
        print box,"-----_box-"
        print v['rotated'], "---rotated"

        sizelist = [ int(x) for x in to_list(v['sourceSize'])]
        rect_on_big = big_image.crop(box)
        '''
        result_image = Image.new('RGBA', sizelist, (0,0,0,0))
        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 v['rotated']:
            result_image = result_image.rotate(90)
        if not os.path.isdir(file_path):
            os.mkdir(file_path)
        outfile = (file_path+'/' + k).replace('gift_', '')
        print result_box,"-----result_box-"
        print outfile, "generated"
        # result_image.save(outfile)
        '''

        if v['rotated']:
            rect_on_big = rect_on_big.rotate(90)
        if not os.path.isdir(file_path):
            os.mkdir(file_path)
        outfile = (file_path+'/' + k).replace('gift_', '')

        rect_on_big.save(outfile);

    if __name__ == '__main__':
    filename = sys.argv[1]
    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"

答案 3 :(得分:0)

感谢Sean.Z.第一。
但是我发现旋转不好,而且ygweric的补丁还不够好 因为当png包含jpg文件时它会失败(模式P)。有时透明会变成黑色背景 我尝试修复此问题并将代码修改如下:

    #! /usr/lical/bin/python
    import os,Image,sys
    from xml.etree import ElementTree

    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_', '')
            print outfile, "generated"
            result_image.save(outfile)

    if __name__ == '__main__':
        filename = sys.argv[1]
        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"

答案 4 :(得分:0)

如果您的PC中有JRE或者您可能正在安装,那么您可以使用我创建的工具来解压缩.plist文件。它还支持在LibGdx项目中使用的.pack文件和在许多引擎中使用的.xml文件,如统一来打包纹理。

https://github.com/itsabhiaryan/TextureUnPacker

相关问题