Automatically clip Android ICON and save to path script

Posted by CybJunior on Thu, 06 Feb 2020 17:19:54 +0100

When packaging Android games or apps, you often encounter problems with dozens or hundreds of channels.app names for different channels are different, and icons may be different.Programmers are always lazy and bother to change things manually.So a script was written in Python to automatically clip icons of different sizes and save them to the icon directory corresponding to Android.

Put a code here and leave a footprint.Use a direct copy later.(Remember to install the pillow third-party library.pip install pillow)

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

# This script function: automatically generate different size icons from a large icon and save them to the corresponding directory

import os
from PIL import Image

# The original icon file name must be in the same directory as the script
ORIGIN_ICON_PATH = "icon.png"
# Output icon directory and icon file name
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")

 

Topics: Android Python pip