您的位置:首页 > 编程语言 > Python开发

将TexturePacker制作的.pvr.ccz和.plist文件还原为多个原图

2017-05-20 09:54 495 查看
第一步,将".pvr.ccz"文件改为“.png”格式
pvr转png.bat

rem path " "里面内容替换为TexturePacker的安装路径,将bat文件放在图片文件夹直接运行就可以了
@ech off

path %path%;"D:\Program Files (x86)\CodeAndWeb\TexturePacker\bin"

for /f "usebackq tokens=*" %%d in (`dir /s /b *.pvr.ccz`) do (
TexturePacker.exe --sheet "%%~dpnd.png" "%%d" --algorithm Basic --allow-free-size --no-trim
)

del out.plist

rem 修改文件名
@python "%~dp0/deletePvr.py" %*

pause


第二步,不知道为啥bat生成的格式“.pvr.png”,没找到好的方法,就用Python写了个改文件名的, 顺便学习一下python

deletePvr.py

#!/usr/bin/python
#-*-coding:utf-8-*-

#批量把文件中".pvr.png"修改为".png"
import os, sys

# 递归修改路径中的".pvr.png"文件
def recursive_rename_files(path):
currentFiles = os.listdir(path) #获取路径的文件
for fileName in currentFiles:
fullFileName = os.path.join(path, fileName)
if fullFileName.endswith(".pvr.png"):
rename_delete_pvr(fullFileName)
if os.path.isdir(fullFileName):
#递归调用
recursive_rename_files(fullFileName)

#修改文件名
def rename_delete_pvr(fileName):
position = fileName.find(".pvr.png")
#字符串截取
resultFileName = fileName[0:position] + fileName[-4:]
#文件重命名
os.rename(fileName, resultFileName)

def main():
currentPath = os.getcwd()
recursive_rename_files(currentPath)

if __name__ == '__main__':
main()
第三步,把大图png,按照“.plist”切成小图,在网上找的代码,然后根据自己的需求改的,顺便加注释

split.py
#!/usr/bin/python
#-*-coding:utf-8-*-
#!python
import os,sys
from xml.etree import ElementTree
from PIL import Image

#匹配字符串末尾是包含某个字符串
def endWith(s,*endstring):
# endswith()匹配字符串的开头或末尾是否包含一个字符串。匹配开头是startswith()
# map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回。
array = map(s.endswith,endstring)
if True in array:
return True
else:
return False

# Get the all files & directories in the specified directory (path).
def get_recursive_file_list(path):
current_files = os.listdir(path)
all_files = []
for file_name in current_files:
full_file_name = os.path.join(path, file_name)
if endWith(full_file_name,'.plist'):
full_file_name = full_file_name.replace('.plist','')
# append() 方法向列表的尾部添加一个新的元素。只接受一个参数。
all_files.append(full_file_name)

if os.path.isdir(full_file_name):
next_level_files = get_recursive_file_list(full_file_name)
# extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
all_files.extend(next_level_files)
return all_files

def tree_to_dict(tree):
d = {}
#enumerate会将数组或列表组成一个索引序列。使我们再获取索引和索引内容的时候更加方便
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', '') #这样做是为了顺便创建一个文件夹
# 判断是否为目录
if not os.path.isdir(file_path):
os.mkdir(file_path) #创建目录

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'])
# 嗯,不是很懂这样的写法,所以用and or写了
# width = int( rectlist[3] if v['rotated'] else rectlist[2] )
# height = int( rectlist[2] if v['rotated'] else rectlist[3] )
width = int(v['rotated'] and rectlist[3] or rectlist[2])
height = int(v['rotated'] and rectlist[2] or rectlist[3])
box=(
int(rectlist[0]),
int(rectlist[1]),
int(rectlist[0]) + width,
int(rectlist[1]) + height,
)
rect_on_big = big_image.crop(box)  #裁剪图片,并获取句柄region
# 这个旋转之后很奇怪,只有中间部分图片,其他的都是空白,不知道是什么原因
# if v['rotated']:
#     rect_on_big = rect_on_big.rotate(90)

# 创建一个新的图片
sizelist = [width, height]
result_image = Image.new('RGBA', sizelist, (0,0,0,0))
result_box=(0, 0, width, height)

result_image.paste(rect_on_big, result_box, mask = 0)
# 图片旋转
if v['rotated']:
# result_image = result_image.rotated(90) 没有这个方法??
result_image =result_image.transpose(Image.ROTATE_90)

# 文件路径修改
testPath = file_path.replace("\\", "/")
position = k.find("/")  #这里还有一个文件夹
if position != -1:
catalogue = testPath + "/" + k[0:position]
if not os.path.isdir(catalogue):
os.mkdir(catalogue)
outfile = (testPath+'/' + k).replace('gift_', '')
print outfile, "generated"
result_image.save(outfile)

if __name__ == '__main__':
currtenPath = os.getcwd()   #该函数不需要传递参数,它返回当前工作目录
allPlistArray = get_recursive_file_list(currtenPath)
print allPlistArray     #数组可以直接输出的
for plist in allPlistArray:
filename = plist
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"


参考:
http://www.tuicool.com/articles/VF7zYz http://blog.csdn.net/tianxiawuzhei/article/details/44923703
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐