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

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


import os.path
import sys
from PIL import Image

"""
自动生成不同分辨率下的App图片
UI设计1080*1920分辨率图片,放在drawable-xxhdpi目录下,自动生成其它的分辨率图片
"""

__author__ = ['"Xitao":<offbye@gmail.com>']


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


  • 8
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

offbye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值