将 hd.png 的图片弄成 .png

现在有这样的一个需求,需要将资源文件中的-hd.png转化成对应的.png文件。因为在新的 cocos2dx 中,已经不再使用后缀的形式的来搜索对应的高清资源文件,而是需要我们把对应的高清文件放到一个专门的目录下面,之后我们可以通过设置资源的搜索路径来搞定。

这里,我写了一个简单的脚本,希望可以帮到大家,默认做的事情就是夺取当前目录下的所有以 png 结尾的文件,然后将 -hd.png 的文件自动重命名为 .png,如果发现有些文件没有对应的高清文件,那么就自动跳过,但是不用担心,我会把漏掉的那些文件显示在终端上,方便我们核对和进行后处理。

代码如下,我也放了一份在 github gist 上。

import os
import re

files = []
for (root, dirnames, filenames) in os.walk('.'):
    if len(filenames) != 0:
        for one in filenames:
            files.append(os.path.join(root, one))

isPng = re.compile(r'.*\.png$')
isHdPng = re.compile(r'.*-hd\.png$')

pngFiles = []
for f in files:
    if isPng.match(f):
        pngFiles.append(f)

hdPngFiles = []
for f in files:
    if isHdPng.match(f):
        hdPngFiles.append(f)

sdPngFiles = []
for f in pngFiles:
    if f not in hdPngFiles:
        sdPngFiles.append(f)

# check whether these two lists cover the same files
excludeFiles = []
for f in sdPngFiles:
    correspondingFile = re.sub(r'\.png$', r'-hd.png', f)
    if correspondingFile not in hdPngFiles:
        print f + ' doesn\'t have a corresponding hd file ' + correspondingFile
        excludeFiles.append(f)

for f in hdPngFiles:
    correspondingFile = re.sub(r'-hd\.png$', r'.png', f)
    if correspondingFile not in sdPngFiles:
        print f + ' doesn\'t have a corresponding normal file ' + correspondingFile
        excludeFiles.append(f)

print
print 'Excluded Files:'
for f in excludeFiles:
    if f in sdPngFiles:
        sdPngFiles.remove(f)
    if f in hdPngFiles:
        hdPngFiles.remove(f)
    print '  ' + f

# replace!
for f in hdPngFiles:
    correspondingFile = re.sub(r'-hd\.png$', r'.png', f)
    os.rename(f, correspondingFile)

print 'Done!'

猜你喜欢

转载自gamedev.iteye.com/blog/1909932
PNG
今日推荐