Android screen resolution adaptation of the icon processing more trouble, let the UI do different size of the picture is also a waste of time, and error prone, so use Python to write a tool to automate the processing of pictures, the UI only need to do a good 1080*1920 resolution of the picture can be, Images of other resolutions are automatically generated.
Import os.pathimport sysfrom PIL import Image "" "Automatically generate app picture UI design 1080*1920 resolution picture at different resolutions, placed in DRAWABLE-XXHDPI directory, Automatic generation of other resolution images "" "__author__ = ['" "Xitao":<[email protected]> ']def image_resize (img_file, target, percent): "" Resize image and save to target path:p Aram img_file:image file path:p Aram Target:save path:p Aram Percent:re Size 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.is Dir (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, per cent) def android (res_dir): Xxhdpi_path = Res_dir + "/drawable-xxhdpi/" if not Os.path.isdir (xxhdpi_path): pri NT ("Xxhdpi_path must be a dir") return-1 path_resize (Xxhdpi_path, Res_dir + '/drawable-xhdpi ', 0.667) path_r Esize (Xxhdpi_path, Res_dir + '/drawable-hdpi ', 0.444) path_resize (Xxhdpi_path, Res_dir + '/drawable-mdpi ', 0.296) if __n ame__ = = "__main__": print (' Please input your androd res dir path ') print (SYS.ARGV) if sys.argv[1]: Androi D (Sys.argv[1])
Automatically generate images at different resolutions of Android