TexturePacker Unity json图集拆解

拿大佬的plist代码过来改了一下,向大佬致敬

使用python27,python需要安装Pillow,直接使用pip安装即可

#!python
import re
import json
import os, sys
from PIL import Image

in_path = 'F:/Ball Blast/Assets/TexturePacker/atlases'
out_path = 'F:/Ball Blast/Assets/TexturePacker/project'

def endWith(s, *endstring):
    array = map(s.endswith, endstring)
    if True in array:
        return True
    else:
        return False
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, '.txt'):
            full_file_name = full_file_name.replace('.txt', '')
            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 to_list(src_dict):
    ret = []
    if src_dict.has_key('x'):
        ret.append(src_dict['x'])
    if src_dict.has_key('y'):
        ret.append(src_dict['y'])
    if src_dict.has_key('w'):
        ret.append(src_dict['w'])
    if src_dict.has_key('h'):
        ret.append(src_dict['h'])
    return ret

def gen_png_from_json(json_filename, png_filename):
    file_path = json_filename.replace('.txt', '')
    big_image = Image.open(png_filename)
    json_dict = json.loads(open(json_filename, 'r').read())
    for k, v in json_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)
        outfile = os.path.join(file_path.replace(in_path, out_path), k)
        outdir = os.path.dirname(outfile)
        if not os.path.isdir(outdir):
            os.makedirs(outdir)
        result_image.save(outfile)
        print 'generate --->' + outfile

if __name__ == '__main__':
    currtenPath = in_path #os.getcwd()
    allJsonArray = get_recursive_file_list(currtenPath)

    for filename in allJsonArray:
        print filename
        json_filename = filename + '.txt'
        png_filename = filename + '_png.png'
        if (os.path.exists(json_filename) and os.path.exists(png_filename)):
            gen_png_from_json(json_filename, png_filename)
        else:
            print "make sure you have boith json and png files in the same directory"

你可能感兴趣的:(Unity)