python切割图片,读取spine的atlas转json配置

# -*- coding: utf-8 -*-
 
import os
import sys
import os.path
import shutil
import Image
import json
 
#fileName = raw_input('输入要解析的文件名:')
 
#if fileName.find('.png') != -1:
#    fileName = fileName[:-4]
 
fileName="spine1"
pngName = fileName + '.png'
atlasName = fileName + '.atlas'
 
print pngName,atlasName
 
big_image = Image.open(pngName)
atlas = file(atlasName, "r");
 
 
curPath = os.getcwd()# 当前路径
aim_path = os.path.join(curPath, fileName)
print aim_path
if os.path.isdir(aim_path):
    shutil.rmtree(aim_path,True)#如果有该目录,删除
os.makedirs(aim_path)
 
#
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();
_line = atlas.readline();


dataDic={}
dataCount=0
dataDic["pics"]=[]
while True:
    line1 = atlas.readline().strip("\n") # name
    if len(line1) == 0:
        break
    else:
        picItem={}
        picItem["name"]=line1

        line2 = atlas.readline() # rotate
        line3 = atlas.readline() # xy
        line4 = atlas.readline() # size
        line5 = atlas.readline() # orig
        line6 = atlas.readline() # offset
        line7 = atlas.readline() # index
        

        rotate=line2.split(":")[1].strip("\n")
        rotate=rotate.strip(" ")
        rotate=rotate=="true"
        print rotate
        if(rotate):
            picItem["rotate"]=1
        else:
            picItem["rotate"]=0


        name = line1.replace("\n","") + ".png";
        
        args = line4.split(":")[1].split(",");
        
        width = int(args[0])
        height= int(args[1])
        if(rotate):
            width=height
            height=int(args[0])

            
        args = line3.split(":")[1].split(",");
        ltx = int(args[0])
        lty = int(args[1])
        picItem["x"]=ltx
        picItem["y"]=lty
        picItem["width"]=width
        picItem["height"]=height

        dataDic["pics"].append(picItem)
        dataCount+=1
            
        rbx = ltx+width
        rby = lty+height
        
        print name,width,height,ltx,lty,rbx,rby
 
        result_image = Image.new("RGBA", (width,height), (0,0,0,0))
        rect_on_big = big_image.crop((ltx,lty,rbx,rby))
        print(rect_on_big)
        result_image.paste(rect_on_big, (0,0,width,height))
        result_image.save(aim_path+'/'+name)
atlas.close()

dataDic["picCount"]=dataCount
with open(fileName+'_atlas.json', 'w') as f:
        json.dump(dataDic, f)
del big_image

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值