TexturePacker是一个把好多小图打成大图的软件,生成的是大图以及小图在大图位置的.plist描述文件,但是不支持把大图还原成小图。网上偷的图一般都是大图和plist,想得到小图比较麻烦,于是乎用python写了个TexturePacker反向工具,把大图导成小图。

  1.python要用到的库

    python的图片处理要用到PIL(Python Image Library),mac10.10下安装PIL会报 fatal error: 'X11/Xlib.h' file not found的错,解决方法在此。而且在装PIL前要先装zlib/libpng/jpeg,安装方法自行百度。

    plist解析用了xml.dom为python自带的库,不用装。

  2.TexturePacker导出的plist结构分析

    plist文件如下所示。 

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>grossini_dance_03.png</key>
<dict>
<key>frame</key>
<string>{{46,324},{63,109}}</string>
<key>offset</key>
<string>{-6,-1}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{5,7},{63,109}}</string>
<key>sourceSize</key>
<string>{85,121}</string>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>2</integer>
<key>realTextureFileName</key>
<string>bbb.png</string>
<key>size</key>
<string>{512,512}</string>
<key>smartupdate</key>
<string>$TexturePacker:SmartUpdate:ea1bbb1419cd4c346debb54e1a7d5de2:1/1$</string>
<key>textureFileName</key>
<string>bbb.png</string>
</dict>
</dict>
</plist> 

    frames对应的value是所有小图的信息。key为小图的名字,dict为小图的信息。

    frame为图片小图位置及大小(这个大小是经过Trim的大小,TP会把png的无像素白边剔除,来减小图片的大小,也就是说大图中小图的大小不一定等与小图真正的大小)。如上plist{{46,324},{63,109}},46和324为小图在大图中x,y坐标,{63,109}为经过裁剪的图片大小,图2其实是png格式黄色背景是空的,为了看着方便,加两个黄色背景。{63,109}是红框内的大小。

TexturePacker大图还原成小图工具带源码-LMLPHP图1TexturePacker大图还原成小图工具带源码-LMLPHP图2TexturePacker大图还原成小图工具带源码-LMLPHP图3

    rotated是是否旋转,这个光头没有旋转,大图中的play按钮有旋转。

    sourceSize为小图的大小。{85, 121}为小图整个大小,没有经过Trim。

    sourceColorRect为经过Trim的图在小图中的起始坐标及大小。{{5,7},{63, 109}}中{5,7}为图2中红框左上角的坐标,{63,109}为大图中小图的大小。

    offset为中心坐标偏移。图3中小红点处为原大小图中心点p1,小红点左边的交叉点为经过Trim的图片中心点p2,以p1为原点,p2的坐标就是这个offset{-6,-1},x向负轴偏移6像素,y向负轴偏移1像素,这里比较奇怪,y轴好像是向上为正了。

   不知这个有offset有神马用,下面的代码没有用offset,用sourceColorRect、sourceSize、frame、rotated就可以确定出小图的样子。

  3.代码分析

    代码中用到了PIL,PIL的参考手册在这,程序中用到了open、new、crop、crop、rotate几个api。

    代码分三个文件,主文件TexturePacker.py,运行此来生成小图。PlistToDict.py来解析plist文件,生成map,key为小图名,value为小图信息。BraceParser.py为{{2,3},{4,5}}生成列表[[2,3][4,5]]。

    知道了plist的含义,稍微会用PIL,代码应该很好理解,代码如下,玩具代码,莫要嘲笑。

    TexturePacker.py,类TextureParser第一个参数是plist及png文件位置,第二个为文件名字。

 import PIL.Image as Image
import BraceParser
import PlistToDict class TextureParser(object):
def __init__(self, path, name):
self.__resPath = path;
self._name = name;
self.__plistDict = PlistToDict.PlistToDict(path + "/" + name + ".plist").createDict(); #return map[picName : {originPoint : {x:, y:}, size : {width:, height:}}]
def __getSmallPicInfos(self):
picInfo = {};
for key, value in self.__plistDict["frames"].items():
size = BraceParser.BraceParser(value["sourceSize"]).createList();
origin = BraceParser.BraceParser(value["frame"]).createList();
sourceSize = BraceParser.BraceParser(value["sourceColorRect"]).createList();
picInfo[key] = {"size" : size,
"origin" : [origin[0][0], origin[0][1]],
"colorOrigin" : [sourceSize[0][0], sourceSize[0][1]],
"colorSize" : [sourceSize[1][0], sourceSize[1][1]],
"isRotated" : value["rotated"]
};
return picInfo; def smallPicsCreate(self, pathToStore = None):
image = Image.open(self.__resPath + "/" + self._name + ".png");
picInfos = self.__getSmallPicInfos();
for k, v in picInfos.items():
if v["isRotated"] == True:
v["size"][0], v["size"][1] = v["size"][1], v["size"][0];
v["colorSize"][0], v["colorSize"][1] = v["colorSize"][1], v["colorSize"][0];
newImage = Image.new("RGBA", (int(v["size"][0]),int(v["size"][1])));
box = (int(v["origin"][0]), int(v["origin"][1]),
int(v["origin"][0] + v["colorSize"][0]), int(v["origin"][1] + v["colorSize"][1]));
region = image.crop(box);
newImage.paste(region, (int(v["colorOrigin"][0]), int(v["colorOrigin"][1])));
if v["isRotated"] == True:
newImage = newImage.rotate(90);
newImage.save(self.__resPath + k); if __name__ == "__main__":
textureUnPacker = TextureParser("/Users/adv/Desktop/", "bbb");
textureUnPacker.smallPicsCreate();
print("success!")

    PlistToDict.py,用的是dom解析plist。dom怎么用自行百度。

 from xml.dom import minidom

 class PlistToDict(object):
def __init__(self, plistPath):
dom = minidom.parse(plistPath);
self.__root = dom.documentElement; # get root dict
def __getFirstDictDoc(self):
children = self.__root.childNodes;
for v in children:
if v.nodeType == v.ELEMENT_NODE and v.nodeName == "dict":
return v;
return None; # get value by key in doc's children
def __getValueDocByKey(self, doc, key):
children = doc.childNodes;
for v in children:
if v.nodeType == v.ELEMENT_NODE and v.nodeName == "key" and v.firstChild.nodeValue == key:
node = v.nextSibling;
while node.nodeType != node.ELEMENT_NODE:
node = node.nextSibling;
if node == None:
return None;
return node;
return None; def __firstElementNodeName(self, doc):
for v in doc.childNodes:
if v.nodeType == v.ELEMENT_NODE:
return v.nodeName; def __docToDict(self, dom, dic):
keys = self.__getAllKeyValuesInDoc(dom);
for key in keys:
valueNode = self.__getValueDocByKey(dom, key);
if valueNode.nodeName == "dict":
dic[key] = {}
self.__docToDict(valueNode, dic[key]);
elif valueNode.nodeName == "false":
dic[key] = False;
elif valueNode.nodeName == "true":
dic[key] = True;
else:
dic[key] = valueNode.firstChild.nodeValue; def __getAllKeyValuesInDoc(self, doc):
ret = [];
for v in doc.childNodes:
if v.nodeName == "key":
ret.append(v.firstChild.nodeValue);
return ret; def createDict(self):
rootDict = self.__getFirstDictDoc();
ret = {};
self.__docToDict(rootDict, ret);
return ret;

    BraceParser.py,用来解析括号。

 class BraceParser(object):
def __init__(self, str):
self.__strToParse = str.replace(" ", ""); def __firstStrIsLeftBrace(self, str):
return True if str[0] == "{" else False; def __subOutBrace(self, str):
return str[1:-1]; def __findAllSeqCommaPos(self, str):
bracketNum = 0;
ret = [];
for i, v in enumerate(str):
if v == "{":
bracketNum += 1;
elif v == "}":
bracketNum -= 1;
elif v == ",":
if bracketNum == 0:
ret.append(i);
return ret; # {111,324},{100,100} return ["{111,324}", "{100,100}"]
def __getAllBraceStrs(self, str):
listStr = [];
posList = self.__findAllSeqCommaPos(str);
lastPos = -1;
for v in posList:
listStr.append(str[lastPos + 1: v]);
lastPos = v;
listStr.append(str[lastPos + 1: ]);
return listStr; def __getValue(self, str):
listStr = str.split(",");
return listStr[0], listStr[1]; def __listCreate(self, str, listIns):
if self.__firstStrIsLeftBrace(str) == True:
braceStrs = self.__getAllBraceStrs(str);
for v in braceStrs:
subList = [];
listIns.append(subList);
self.__listCreate(self.__subOutBrace(v), subList);
else:
x, y = self.__getValue(str);
listIns.append(float(x));
listIns.append(float(y)); def createList(self):
listIns = [];
str = self.__subOutBrace(self.__strToParse);
self.__listCreate(str, listIns);
return listIns;

    最后,我想问博客园怎么上传附件?

05-11 09:36