自动生成Android不同分辨率下的图片

转自: http://blog.csdn.net/offbye/article/details/50012605

Android屏幕分辨率适配的图标处理比较麻烦,让UI做不同尺寸的图片也挺浪费时间的,并且容易出错,于是用Python写了个工具自动化处理图片,UI只需要做好1080*1920分辨率下的图片就可以了,其它分辨率的图片自动生成。


[python]  view plain  copy
  1. import os.path  
  2. import sys  
  3. from PIL import Image  
  4.   
  5. """ 
  6. 自动生成不同分辨率下的App图片 
  7. UI设计1080*1920分辨率图片,放在drawable-xxhdpi目录下,自动生成其它的分辨率图片 
  8. """  
  9.   
  10. __author__ = ['"Xitao":<offbye@gmail.com>']  
  11.   
  12.   
  13. def image_resize(img_file, target, percent):  
  14.     """resize image and save to target path 
  15.     :param img_file: image file path 
  16.     :param target: save path 
  17.     :param percent: resize percent 
  18.     :return: 
  19.     """  
  20.     img = Image.open(img_file)  
  21.     print(img.size)  
  22.     width, height = img.size  
  23.     target_img = img.resize((int(width * percent), int(height * percent)), Image.ANTIALIAS)  
  24.     target_img.save(target)  
  25.     img.close()  
  26.     target_img.close()  
  27.     print(" save target image to " + target)  
  28.   
  29.   
  30. def path_resize(src, target, percent):  
  31.     if not os.path.isdir(src):  
  32.         print(src + " must be a dir")  
  33.         return -1  
  34.   
  35.     os.chdir(src)  
  36.     cwd = os.getcwd()  
  37.     dirs = os.listdir(cwd)  
  38.     for file_name in dirs:  
  39.         print file_name  
  40.         if file_name.endswith('.9.png'):  
  41.             continue  
  42.   
  43.         src_file = os.path.join(cwd, file_name)  
  44.   
  45.         if not os.path.exists(target):  
  46.             os.mkdir(target)  
  47.         image_resize(src_file, target + '/' + file_name, percent)  
  48.   
  49.   
  50. def android(res_dir):  
  51.     xxhdpi_path = res_dir + "/drawable-xxhdpi/"  
  52.   
  53.     if not os.path.isdir(xxhdpi_path):  
  54.         print("xxhdpi_path must be a dir")  
  55.         return -1  
  56.   
  57.     path_resize(xxhdpi_path, res_dir + '/drawable-xhdpi'0.667)  
  58.     path_resize(xxhdpi_path, res_dir + '/drawable-hdpi'0.444)  
  59.     path_resize(xxhdpi_path, res_dir + '/drawable-mdpi'0.296)  
  60.   
  61.   
  62. if __name__ == "__main__":  
  63.     print('please input your androd res dir path')  
  64.     print(sys.argv)  
  65.     if sys.argv[1]:  
  66.         android(sys.argv[1])  
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值