標籤:
Android螢幕解析度適配的表徵圖處理比較麻煩,讓UI做不同尺寸的圖片也挺浪費時間的,並且容易出錯,於是用Python寫了個工具自動化處理圖片,UI只需要做好1080*1920解析度下的圖片就可以了,其它解析度的圖片自動產生。
import os.pathimport sysfrom PIL import Image"""自動產生不同解析度下的App圖片UI設計1080*1920解析度圖片,放在drawable-xxhdpi目錄下,自動產生其它的解析度圖片"""__author__ = [‘"Xitao":<[email protected]>‘]def image_resize(img_file, target, percent): """resize image and save to target path :param img_file: image file path :param target: save path :param percent: resize percent :return: """ img = Image.open(img_file) print(img.size) width, height = img.size target_img = img.resize((int(width * percent), int(height * percent)), Image.ANTIALIAS) target_img.save(target) img.close() target_img.close() print(" save target image to " + target)def path_resize(src, target, percent): if not os.path.isdir(src): print(src + " must be a dir") return -1 os.chdir(src) cwd = os.getcwd() dirs = os.listdir(cwd) for file_name in dirs: print file_name if file_name.endswith(‘.9.png‘): continue src_file = os.path.join(cwd, file_name) if not os.path.exists(target): os.mkdir(target) image_resize(src_file, target + ‘/‘ + file_name, percent)def android(res_dir): xxhdpi_path = res_dir + "/drawable-xxhdpi/" if not os.path.isdir(xxhdpi_path): print("xxhdpi_path must be a dir") return -1 path_resize(xxhdpi_path, res_dir + ‘/drawable-xhdpi‘, 0.667) path_resize(xxhdpi_path, res_dir + ‘/drawable-hdpi‘, 0.444) path_resize(xxhdpi_path, res_dir + ‘/drawable-mdpi‘, 0.296)if __name__ == "__main__": print(‘please input your androd res dir path‘) print(sys.argv) if sys.argv[1]: android(sys.argv[1])
自動產生Android不同解析度下的圖片