将 hd.png 的图片弄成 .png

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

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

代码如下,我也放了一份在 [url=https://gist.github.com/guanqun/5629270]github gist[/url] 上。


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!'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值