Cocos 原生Android经常需要替换icon,在Android项目中的icon一般都放在 res 文件夹中的子项中,手动替换需要对比大小,再拷贝.
此脚本是在Android项目res同级目录下,需要替换的icon也需要拷贝到同级目录下
思路:先去res中递归找需要替换的icon,找到后,获取icon大小,再去需要替换的icon库中找相同大小的icon
删除源路径的icon,再将icon库的图片拷贝到该目录下
修改拷贝后的icon的名字
目前使用到的是 pillow 库
#!usr/bin/python3 # -*- coding=utf8 -*- import os import shutil from PIL import Image #width,height,name iconfile = r‘icon‘ resfile = r‘res‘ logfile = open(‘log.txt‘,‘w‘,encoding=‘utf-8‘) def readiconfile(iconpath,respath,resw,resh): for file in os.listdir(iconpath): fs = os.path.splitext(file) file_path = os.path.join(iconpath,file) if ‘.png‘ == fs[1]: img = Image.open(file_path) width,height = img.size img.close() if resw == width and resh == height: logfile.write("readiconfile remove respath:" + respath + ‘\n‘) copyafname = respath + ‘\\‘ + file rename = respath + ‘\\‘ + ‘icon.png‘ os.remove(rename) shutil.copy(os.path.join(iconpath,file),respath) logfile.write("copyafname:" + copyafname + ‘\n‘) logfile.write("rename:" + rename + ‘\n‘) os.rename(copyafname,rename) def readres(path): for file in os.listdir(path): file_path = os.path.join(path,file) logfile.write("readres file_path:" + file_path + ‘\n‘) if os.path.isdir(file_path): readres(file_path) else: fs = os.path.splitext(file) if ‘icon‘ == fs[0] and ‘.png‘ == fs[1]: img = Image.open(file_path) width,height = img.size img.close() readiconfile(iconfile,path,width,height) if __name__ == ‘__main__‘: readres(resfile) logfile.close()
这里在读取需要替换的icon的时候使用的是每次都会去循环找一次,但是没有找到怎么存储它的方法。所以只能用最挫的代码先解决问题。
原来想的是有没有像二维数组一样的内容来存储icon信息,这样只要读取一次就可以了,后面再改
问题:
1、使用img=Image.open()之后需要关闭,否则会提示该文件正在使用
2、路径都是相对脚本文件所在位置的绝对路径,而且操作中记得路径的拼接
原文:https://www.cnblogs.com/crhui/p/14806729.html