Automatically generate images at different Android resolutions
It is troublesome to process the icons for Android screen resolution adaptation, which makes it a waste of time to make images of different sizes on the UI and is prone to errors. Therefore, I wrote a tool in Python to automate image processing, the UI only needs to prepare an image with a resolution of 1080*1920. Images with other resolutions are automatically generated.
Import OS. pathimport sysfrom PIL import Image "automatically generates App images of different resolutions. uidesign: 1080*1920 resolution images, which are placed in the drawable-xxhdpi directory, automatic Generation of other resolution images "" _ author _ = ['"Xitao ":
'] 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])