自动裁剪Android ICON并保存到对应路径脚本

    在给Android游戏或app打包时,经常会遇到有几十上百个渠道的问题。不同的渠道app的名字不一样,icon也可能不一样。程序员总是懒的,最烦手动去改来改去。因此用Python写了一个自动裁剪不同尺寸的icon,并保存到Android对应的icon目录下的脚本。

    这里贴下代码,留个脚印。日后直接拷贝使用。(记得安装下pillow第三方库。pip install pillow)

#!/usr/bin/python
# -*- coding:utf-8 -*-

# 此脚本功能:根据一张大的icon自动生成不同尺寸的icon,并保存到对应目录下

import os
from PIL import Image

# 原始icon文件名,必须与脚本同目录下
ORIGIN_ICON_PATH = "icon.png"
# 输出icon的目录和icon文件名
OUTPUT_ICON_PATH = "proj.android/res/drawable-"
OUTPUT_ICON_NAME = "icon"
ICON_SIZE_DICT = {"ldpi": (36, 36), "mdpi": (48, 48), "hdpi": (72, 72), "xhdpi": (96, 96), "xxhdpi": (114, 114)}


def make_more_images():
    if not os.path.isfile(ORIGIN_ICON_PATH) and not os.path.exists(ORIGIN_ICON_PATH):
        print ORIGIN_ICON_PATH + " is NOT exist."
        return
    im = Image.open(ORIGIN_ICON_PATH)
    print "origin size: " + im.size.__str__()
    if im.size[0] != im.size[1]:
        print "width and height MUST be same."
        return
    if im.size[0] < ICON_SIZE_DICT.get("xxhdpi")[0]:
        print "image size is TOO small."
        return
    for dpi, size in ICON_SIZE_DICT.items():
        path = OUTPUT_ICON_PATH + dpi
        if not os.path.isdir(path):
            print "make [" + dpi + ": " + size.__str__() + "] fail. why: "\
                  + os.path.basename(path) + " is invalid folder."
            continue
        path = path + "/" + OUTPUT_ICON_NAME + ".png"
        if os.path.isfile(path) and os.path.exists(path):
            os.remove(path)
        im.resize(size).save(path, "PNG")
        print "make [" + dpi + ": " + size.__str__() + "] success."


if __name__ == "__main__":
    print '======MAKE ICON START======'
    make_more_images()
    print '=======MAKE ICON END======='
    os.system("pause")

 

你可能感兴趣的:(Python,android,cocos2d-x)