首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从plist文件中提取png图像?

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

Stack Overflow用户
提问于 2011-05-22 07:21:51
回答 4查看 10.3K关注 0票数 0

我现在有一组plist文件。里面是png图像,有些文件有很多图像。我用的是win64 vista。

我专门为这类产品寻找了一些东西,比如FileJuicer,但这只适用于mac用户。

EN

回答 4

Stack Overflow用户

发布于 2012-08-13 07:14:09

我编写了这个脚本来解压缩由png打包的plist文件中的TexturePacker文件。

首先,确保png和plist文件位于同一个目录中,在我的示例中是: freeGifts.plist和freeGifts.png。

第二,以下脚本与unpack_plist.py相同

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

需求: python,PIL

代码语言:javascript
运行
复制
#! /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"
票数 3
EN

Stack Overflow用户

发布于 2011-05-22 07:49:42

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

票数 0
EN

Stack Overflow用户

发布于 2013-07-24 15:35:13

感谢Sean.Z。

对于unpack_plist.py,您需要:

1: PIL已安装在您的电脑上。

2:使用python 2.5

python2.5 unpack_plist.py birdfly

否则你会失败的。

--2013-07-25--

对于一些旋转的褶皱,上面的文字可能有一些错误。我将其修改如下:

代码语言:javascript
运行
复制
#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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6086615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档