Python切割图集

在这里插入图片描述在这里插入图片描述在这里插入图片描述

使用shoebox

官网地址: http://renderhjs.net/shoebox/
在这里插入图片描述
在这里插入图片描述

Json格式

# -*- coding: utf-8 -*-

import os,sys
import json
import os
import os.path
from PIL import Image

def json_to_dict(json_filename):
    json_file = open(json_filename, 'r')
    all_pic_dic = json.load(json_file)
    all_item_list = []
    for one_pic_item in all_pic_dic['res']:
        one_json_item = all_pic_dic['res'][one_pic_item]
        one_item = {}
        one_item['name'] = one_pic_item.strip().lstrip().rstrip(',')
        one_item['x'] = one_json_item['x']
        one_item['y'] = one_json_item['y']
        one_item['w'] = one_json_item['w']
        one_item['h'] = one_json_item['h']
        all_item_list.append(one_item)

    return all_item_list
       
   

def gen_png_from_json(folder_name, json_filename, png_filename):
    big_image = Image.open(png_filename)
    all_item_list = json_to_dict(json_filename)

    print 'gen_png_from_json:' + folder_name

    #清理掉原目录
    if not os.path.isdir(folder_name):
        #os.removedirs(folder_name)
        os.mkdir(folder_name)

    for i, one_item_data in enumerate(all_item_list):
        file_name = one_item_data['name']
        x = one_item_data['x']
        y = one_item_data['y']
        w = one_item_data['w']
        h = one_item_data['h']

        #设置图像裁剪区域 (x左上,y左上,x右下,y右下)
        image_box = [x, y, x + w , y + h ]
        one_pic = big_image.crop(image_box)

        one_pic.save(folder_name + "/" + file_name + '.png') # 存储裁剪得到的图像
        
        #print one_item_data

plist格式

# -*- co
#!python
import os,sys
from xml.etree import ElementTree
from PIL import Image

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)

注:依赖PIL,本文使用[Python Imaging Library 1.1.7 for Python 2.7]
官网 : http://www.pythonware.com/products/pil/
安装完后执行

		cd C:\Python27\scripts\
		pip install pillow

完整示例: https://github.com/l2xin/UnpackSpriteSheet


Github:https://github.com/l2xin
qq群:215974591,欢迎加群共同探讨学习。
个人微信公众号:牵蜗牛看世界
在这里插入图片描述

### 回答1: Python的Atlas图集拆分可以用来将大型的图集文件切分成较小的单个图片文件,以便方便地使用它们在不同的游戏引擎或应用程序中。以下是关于如何使用Python进行Atlas图集的拆分。 首先,需要安装Pillow库和xml.etree.ElementTree库,这可以通过在终端中运行“pip install pillow”和“pip install xml.etree.ElementTree”来完成。这两个库都是Python图像处理和XML解析的标准库。 接下来,需要准备Atlas图集,并将其保存为XML格式。我们可以使用一些图集编辑器(如TexturePacker)来创建Atlas图集,并导出为XML格式。 在Python脚本中,可以使用xml.etree.ElementTree库来解析XML文件,然后提取每个纹理的位置和大小。然后,可以使用Pillow库来裁剪图像数据,并将其保存到单独的输出文件中。 最后,将所有的单独的图像合并成一个Atlas图集,以便在应用程序中使用。可以使用TexturePacker等工具来执行此操作,也可以使用代码来实现。 总之,Python Atlas图集拆分是一个很有用的工具,可以简化图像管理和优化应用程序性能。通过使用Pillow和xml.etree.ElementTree库,可以实现这个功能,并获得更好的控制和灵活性。 ### 回答2: Python Atlas图集拆分通常是针对游戏引擎和其他需要大量图像资源的应用程序进行的,它将大型图像文件分解为小图像块,以便在需要时加载和使用图像资源。 要使用Python拆分图集,我们可以使用Pillow库或OpenCV库等图像处理库来实现。我们首先需要将含有所有图像的大型图像文件加载到Python中。然后,我们可以使用库中可用的裁剪函数来划分图像块。我们需要提供裁剪参数,例如每个块的大小,每个块之间的间隔,以及需要裁剪的图像的位置。然后将块保存到单独的文件中,以便我们可以随时在程序中读取和使用它们。 我们还可以使用Python脚本来自动化此过程,以便同时处理多个图像文件。我们可以编写一个Python脚本来迭代处理文件夹中的所有图像文件,并将它们拆分为块。这样可以节省时间和精力,并使我们能够快速创建大量的图像资源。 在实际应用中,我们通常需要执行额外的处理步骤,例如为拆分的图像块打标记和元数据或重新整理图像文件的名称和路径。这些都可以通过Python脚本和其他库来实现。 总之,Python Atlas图集拆分是一种方便快捷的方式来处理大量图像资源。它可以帮助增加程序的性能,减少加载时间,并提高用户体验。 ### 回答3: Atlas图集(也称为纹理图集)是一种将多个小型纹理合并成单个大型纹理的技术。这种技术可以显著减少内存使用量,因此成为许多游戏和应用程序的常见技术。 在Python中,有许多库可以帮助我们拆分Atlas图集。其中一种流行的库是Pygame,在Pygame中,我们可以使用Surface.subsurface(rect)方法从Atlas图集中提取小型纹理。 此外,还有一些第三方库可用于Atlas图集拆分,例如Pillow和OpenCV。这些库提供了更多的功能和配置选项,使我们可以更轻松地进行Atlas图集拆分。 在使用这些库拆分Atlas图集时,我们需要先找到Atlas图集中各个小型纹理的矩形区域。这可以通过阅读Atlas图集中的元数据文件来完成,或者使用图像处理算法来寻找区域。一旦我们找到了这些区域,就可以使用所选的库从Atlas图集中提取小型纹理,并将它们保存到单独的图像文件中以供使用。 总体而言,Atlas图集拆分是一项重要的任务,它可以帮助我们提高游戏和应用程序的性能和内存使用。使用Python和适当的库,我们可以轻松地进行Atlas图集拆分,并从中获得许多好处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值