自動裁剪Android ICON並儲存到對應路徑指令碼
阿新 • • 發佈:2019-01-23
在給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")