在项目制作的过程中,有可能会出现一张图片需要有不同的大小去适应不同的设备和位置时,手动去一个个制作比较麻烦,本人通过网上学习粗略敲了一段python脚本,可能有考虑不周之处还请指出。文底有下载链接
old_file = ""
new_file=""
new_format="RGB"
def create_image(pri_image):
for image in control["images"]:
if(image['filename'][0] == 'I'):
new_format = "RGB"
else:
new_format = "RGBA"
indexofx = find_last(image["size"],'x')
newsize = int(float(image["size"][:indexofx]) * int(image["scale"][0]))
pri_image.convert(new_format).resize((newsize, newsize), Image.ANTIALIAS).save(new_file + image["filename"])
def savecontents():
content = new_file+'Content.json'
json.dump(control,open(content,'w'),indent=4)
def get_file():
global old_file
old_file = raw_input("resource:")
print("old_file: %s" % old_file)
return Image.open(old_file)
def newfilepath():
global new_file
inx=find_last(old_file,'\\')
if(os.path.exists(old_file[:inx+1]+"AppIcon.appiconset") == False):
os.mkdir(old_file[:inx+1]+"AppIcon.appiconset")
new_file = old_file[:inx+1]+"AppIcon.appiconset\\"
print("new_file: %s" % new_file)
def outfilename():
for i in control["images"]:
print(i)
def find_last(string,str):
last_position = -1
while True:
position = string.find(str,last_position+1)
if position == -1:
return last_position
last_position=position
if __name__ == "__main__":
pri_image = get_file()
newfilepath()
create_image(pri_image)
savecontents()
print("finish")
icon生成工具使用说明:
1.使用:
一、双击icon生成工具脚本或拖拽到cmd回车
二、拖拽素材图片到生成的窗口
三、生成对应压缩包在素材文件下
2.可能出现的问题
一、没有安装python2
www.python.org/
二、No module named _curses
pip install windows-curses
没有pip库就安装pip库、也有可能pip没有添加到系统变量
三、报错The _imaging C module is not installed
没有PIL库或PIL库与当前python不匹配
pip uninstall Pillow
pip install Pillow
格式备注:
1:1位像素,表示黑和白,但是存储的时候每个像素存储为8bit。二值化图 \n
L:8位像素,表示黑和白。 灰度图 \n
P:8位像素,使用调色板映射到其他模式。\n
RGB:3x8位像素,为真彩色。\n
RGBA:4x8位像素,有透明通道的真彩色。\n
CMYK:4x8位像素,颜色分离。\n
YCbCr:3x8位像素,彩色视频格式。\n
I:32位整型像素。\n
F:32位浮点型像素。")