Automatically generate images at different Android resolutions

Source: Internet
Author: User

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])
 


Related Article

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.