Automatically generate images at different resolutions of Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.