写这片博客前,我在学习cocos2d-x,正在做一个微信打飞机的项目,为了将飞机图片资源合成为一张,使用到了TexturePackerGUI软件,在这介绍使用方法,后面是将这张合成图片分解成原来图片的方法。
破解完成之后就开始使用,对里面控件不熟悉的请参考这篇文章,虽然看起来复杂,但是只要把你所有需要合成的资源全部移到右边的框内就行了,如下图(蓝色的地方):
好了,把图片全部拖进去之后,就可以开始导出了,选择菜单栏中的Publish,在此之前,请在左边的DataFile中设置导出路径。
好了,图片合成完成 了,你将会获得到一个png文件和一个plist文件。
将这些图片导入到cocos2d中就可以直接使用喽,是不是很方便,不过合成之前的每一个小图片的名字要记住哦,不然cocos2d里你就不知道该使用哪一个了。
OK,有合成就有分解,分解起来可是快多了。
首先你需要下载一个名为Python Imaging Library的软件,下载链接,当然,你的电脑之前要装python,然后选择和你电脑python版本适配的PIL下载。
之后你需要一个split_png_plist.py文件,这个文件可以用记事本自己写,代码如下:
#! /usr/lical/bin/python
import os,Image,sys
from xml.etree import ElementTree
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():
print "-----start\n----------"
rectlist = to_list(v['frame'])
print rectlist, "--------rectlist"
width = int( rectlist[3] if v['rotated'] else rectlist[2] )
height = int( rectlist[2] if v['rotated'] else rectlist[3] )
print width,height,"----width,height"
box=(
int(rectlist[0]),
int(rectlist[1]),
int(rectlist[0]) + width,
int(rectlist[1]) + height,
)
# bos is start & end point
print box,"-----_box-"
print v['rotated'], "---rotated"
sizelist = [ int(x) for x in to_list(v['sourceSize'])]
rect_on_big = big_image.crop(box)
'''
result_image = Image.new('RGBA', sizelist, (0,0,0,0))
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 v['rotated']:
result_image = result_image.rotate(90)
if not os.path.isdir(file_path):
os.mkdir(file_path)
outfile = (file_path+'/' + k).replace('gift_', '')
print result_box,"-----result_box-"
print outfile, "generated"
# result_image.save(outfile)
'''
if v['rotated']:
rect_on_big = rect_on_big.rotate(90)
if not os.path.isdir(file_path):
os.mkdir(file_path)
outfile = (file_path+'/' + k).replace('gift_', '')
if not outfile.lower().endswith('.png'): #PIL fails if no extension
outfile += ".png";
print "saving:" + outfile;
rect_on_big.save(outfile);
print "saved:" + outfile;
if __name__ == '__main__':
filename = sys.argv[1]
plist_filename = filename + '.plist'
png_filename = filename + '.png'
if (os.path.exists(plist_filename) and os.path.exists(png_filename)):
gen_png_from_plist( plist_filename, png_filename )
else:
print "make sure you have boith plist and png files in the same directory"
OK,图片的合成和分解就这么华丽丽的完成了!
如果有小伙伴需要看看我的微信打飞机的资源的话请移步
好的,这篇文章到这里就结束啦。
关于使用TexturePackerGUI将图片合成和用Python Imaging Library将图片分解,布布扣,bubuko.com
关于使用TexturePackerGUI将图片合成和用Python Imaging Library将图片分解
原文地址:http://blog.csdn.net/q100036q/article/details/32699555