123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- # -*- coding: utf-8 -*-
- from lib2to3.pytree import Node
- from math import floor
- import os
- import shutil
- import subprocess
- import xml.etree.ElementTree as ElementTree
- import plistlib
- #检查是否存在目录,没有则创建
- def checkDirectoryOrCreate(dir):
- if not os.path.exists(dir):
- os.makedirs(dir)
- #比较name中是否在suffix格式列表里
- #suffix为list,例如:['.png', '.mp3'],如suffix为None或者大小为0则直接返回True
- def __validExtension(name, suffix):
- if None == suffix:
- return True
- if len(suffix) <= 0:
- return True
- name = name.lower()
- for suffixTemp in suffix:
- if name.endswith(suffixTemp.lower()):
- return True
- return False
- #查找目录下指定类型的文件,忽略大小写
- #suffixList(数组)指定文件拓展名,例如:findFiles('../dir', False, ['.png', '.mp3']),如suffix为None或者大小为0则不过滤任何格式
- def findFiles(directory, includeChild, suffixList):
- ret = []
- if includeChild:
- for root, dirs, files in os.walk(directory):
- for name in files:
- if name.startswith('.'):
- continue
- if None == suffixList:
- temp = os.path.join(root, name)
- ret.append(temp)
- continue
- for suffixTemp in suffixList:
- if name.lower().endswith(suffixTemp.lower()):
- temp = os.path.join(root, name)
- ret.append(temp)
- break
- else:
- dirList = os.listdir(directory)
- for name in dirList:
- if name.startswith('.'):
- continue
- if None == suffixList:
- temp = os.path.join(directory, name)
- ret.append(temp)
- continue
- for suffixTemp in suffixList:
- if name.lower().endswith(suffixTemp.lower()):
- temp = os.path.join(directory, name)
- ret.append(temp)
- break
- return ret
- #搜集directory下的文件,拷贝到tarDirectory目录,注意tarDirectory里不保持与directory相同的目录结构
- #suffix(数组)指定文件拓展名,例如:copyFilesToDirectory('../dir', False, ['.png', '.mp3']),如suffix包含‘.*’则不过滤任何格式
- def copyToDirectory(directory, tarDirectory, includeChild, suffix):
- allFiles = findFiles(directory, includeChild, suffix)
- for curFile in allFiles:
- _, desFileName = os.path.split(curFile)
- desFile = os.path.join(tarDirectory, desFileName)
- shutil.copyfile(curFile, desFile)
- print(os.path.abspath(curFile))
- return len(allFiles)
- #指定Tps文件,调整缩放倍数,重新导出到指定目录下,如执行失败返回错误码
- def scaleTps(tpsFile, targetDir, scaleFactor):
- tpsFile = os.path.abspath(tpsFile)
- tree = ElementTree.parse(tpsFile)
- root = tree.getroot()
- textureFilePath = ''
- atalsFilePath = ''
- scaleRatio = 1.0;
- st = root.find('struct').getchildren()
- childCnt = len(st)
- for idx in range(0, childCnt):
- child = st[idx]
- if 'textureFileName' == child.text:
- textureFilePath = st[idx + 1].text
- elif 'dataFileName' == child.text:
- atalsFilePath = st[idx + 1].text
- elif 'dataFileNames' == child.text:
- atalsFilePath = st[idx + 1][1][1].text
- _, textureFileName = os.path.split(textureFilePath)
- _, atlasFileName = os.path.split(atalsFilePath)
- targetDir = os.path.abspath(targetDir)
- newTextureFile = os.path.join(targetDir, textureFileName)
- newAtalsFile = os.path.join(targetDir, atlasFileName)
- #获取原来的缩放系数
- for struct in root.iter('struct'):
- type = struct.attrib['type']
- if(type == 'SpriteSettings'):
- st = struct.getchildren()
- childCnt = len(st)
- for idx in range(0, childCnt):
- child = st[idx]
- if 'scale' == child.text:
- scaleRatio = float(st[idx + 1].text)
- print(scaleFactor*scaleRatio)
- cmd = "TexturePacker '%s' --scale %s --data '%s' --sheet '%s'" % (tpsFile, scaleFactor, newAtalsFile, newTextureFile)
- ret = os.system(cmd)
- if os.path.exists(newAtalsFile):
- print(atlasFileName)
- ext = os.path.splitext(atlasFileName)
- strExt = ext[len(ext) - 1]
- strExt = strExt.lower()
- if strExt == '.plist':
- f = open(newAtalsFile, 'rb')
- plistData = plistlib.load(f)
- #plistData['metadata']['scaleRatio'] = str(scaleFactor)
- plistData['metadata']['scaleRatio'] = '1'
- f.close()
- f = open(newAtalsFile, 'wb')
- plistlib.dump(plistData, f)
- f.close()
- return ret
- def scalePic(scaleFactor, inputFile, outputFile):
- inputFile = os.path.abspath(inputFile)
- outputFile = os.path.abspath(outputFile)
- cmd = "sips -g pixelWidth -g pixelHeight '%s'" % inputFile
- ret = os.popen(cmd).read().splitlines()
- tmp = ret[1].split(':')
- width = int(tmp[1]) * scaleFactor
- width = floor(width)
- tmp = ret[2].split(':')
- height = int(tmp[1]) * scaleFactor
- height = floor(height)
- if width < 1:
- width = 1
- if height < 1:
- height = 1
- cmd = "sips -z %d %d '%s' -o '%s'" % (height, width, inputFile, outputFile)
- os.system(cmd)
-
- def scalePicToDir(scaleFactor, inputFile, outputDir):
- _, fileName = os.path.split(inputFile)
- newFile = os.path.join(outputDir, fileName)
- scalePic(scaleFactor, inputFile, newFile)
- #缩放tps或者图片到指定的目录下
- def scaleTpsOrPicToDir(scaleFactor, searchDir, targetDir, includeChild):
- fileList = findFiles(searchDir, includeChild, ['.tps', '.png', '.jpg'])
- for curFile in fileList:
- _, ext = os.path.splitext(curFile)
- ext = ext.lower()
- if '.tps' == ext:
- scaleTps(curFile, targetDir, scaleFactor)
- else:
- scalePicToDir(scaleFactor, curFile, targetDir)
|