1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- from PIL.Image import Resampling
- import xml.etree.ElementTree as ET
- from PIL import Image
- import os
- from print_log import printlog
- def change_icon_size(image_path, drawablePath, width, height):
- if os.path.exists(drawablePath):
- im = Image.open(image_path)
- out = im.resize((int(width), int(height)), Resampling.LANCZOS)
- out.save(drawablePath, im.format)
- printlog('[drawablePath] : %s \n replace completed!' % drawablePath)
- pass
- def replace_icon(temp_dir, icon_path):
- am_path = os.path.join(temp_dir, 'AndroidManifest.xml')
- allIconSize = {'drawable': '512', 'drawable-ldpi': '72', 'drawable-mdpi': '96', 'drawable-hdpi': '192',
- 'drawable-xhdpi': '256'
- , 'drawable-xxhdpi': '384', 'drawable-xxxhdpi': '512','drawable-ldpi-v4':'36','drawable-hdpi-v4': '72',
- 'drawable-xhdpi-v4': '256', 'drawable-xxhdpi-v4': '384', 'drawable-xxxhdpi-v4': '512',
- 'mipmap-ldpi': '36', 'mipmap-mdpi': '96', 'mipmap-hdpi': '192', 'mipmap-xhdpi': '256',
- 'mipmap-xxhdpi': '384', 'mipmap-xxxhdpi': '512'
- }
- roundIconSize = {'mipmap-ldpi': '36', 'mipmap-mdpi': '96', 'mipmap-hdpi': '192', 'mipmap-xhdpi': '256',
- 'mipmap-xxhdpi': '384', 'mipmap-xxhdpi-v4': '384', 'mipmap-xxxhdpi': '512'}
- ret, icon_name = get_icon_name(am_path)
- for iconDir, iconSize in allIconSize.items():
- drawablePath = os.path.join(temp_dir, 'res', iconDir, "%s.png" % icon_name)
- change_icon_size(icon_path, drawablePath, iconSize, iconSize)
- for iconDir, iconSize in roundIconSize.items():
- drawablePath = os.path.join(temp_dir, 'res', iconDir, "%s_round.png" % icon_name)
- change_icon_size(icon_path, drawablePath, iconSize, iconSize)
- def get_icon_name(am_path):
- if not os.path.exists(am_path):
- return False, "AndroidManifest.xml is not exists!"
- namespace = "http://schemas.android.com/apk/res/android"
- amTree = ET.parse(am_path)
- amRoot = amTree.getroot()
- applicationNode = amRoot.find("application")
- iconName = applicationNode.attrib.get("{%s}icon" % namespace)
- if iconName is None:
- return False, "cant find icon name"
- iconName = iconName.split("/")[1]
- return True, iconName
- pass
|