python =》分割图集(.png/.plist)

原因:
1,因再网上扒了一套游戏图,资源都是 .png/plist 的   cocos creator 识别出错
2,在其他地方找了很多,记得有个一款 shoebox 工具导进去就能切割的 找不到了?新的需要安装adobe air  放弃、、
3,网上还有 atlas/plist 的  但是咱的是 png/plist、、、

找了一款python代码 进行分割  for mac

注意事项:
1,mac 自带python  2.7 提示已经放弃维护了  请使用 更高级版本
2,安装后  请使用  pip3  install packageName     执行请使用:python3   test.py  无需切换或者删除自带包 

安装包:安装第三方库Pillow,biplist
1)pip3  install  biplist
2)pip3  install  pillow

执行:python3   test.py

import os
import re
from biplist import readPlist
from PIL import Image


def split_plist_atlas(path):
    image = Image.open(os.path.splitext(path)[0]+".png")
    plist = readPlist(os.path.splitext(path)[0]+".plist")
    prefix = os.path.dirname(path)+os.sep + "Sprites_" + \
        get_file_name(path) + os.sep
    frames: dict = plist["frames"]
    regex = re.compile("({|})")
    for k in frames:
        filename = prefix + k.replace("jpg", "png")
        rectKey = "frame" if "frame" in frames[k] else "textureRect"
        rect = regex.sub("", frames[k][rectKey]).split(",")
        rect = [int(v) for v in rect]
        rotateKey = "rotated" if "rotated" in frames[k] else "textureRotated"
        rotate: bool = frames[k][rotateKey]
        x = rect[0]
        y = rect[1]
        w = rect[0] + rect[2] if not rotate else rect[0] + rect[3]
        h = rect[1] + rect[3] if not rotate else rect[1] + rect[2]
        box = (x, y, w, h)
        img = image.crop(box)
        if not os.path.exists(prefix):
            os.makedirs(prefix)
        if rotate:
            img = img.rotate(90, expand=1)
        img.save(filename)
        print(filename)


def get_file_name(path):
    filename: str = os.path.split(path)[-1]
    index = filename.rfind(".")
    return filename[0:index]


while True:
    path = input("请将图片资源或plist文件拖到此处:").strip()
    print()
    if os.path.exists(path):
        if os.path.exists(os.path.splitext(path)[0]+".plist"):
            try:
                split_plist_atlas(path)
                print("\nOver\n")
            except Exception as e:
                print("Exception ", e, "\n")
                print("文件解析出错,请重试\n")
        else:
            print("图片资源或plist文件未找到\n")
    else:
        print("\n文件路径异常,请重试\n")




代码转:https://blog.csdn.net/qq_23724267/article/details/103186735  谢谢博主 如有侵权望告知

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值