Python 适配android左右布局图片

res/
  drawable/
    a.png  
  drawable-ldrtl/
    a.png  // 对标 drawable/a.png 的 RTL 图标
  drawable-xhdpi/
    b.png  
  drawable-ldrtl-xhdpi/
    b.png  // 对标 drawable-xhdpi/b.png 的 RTL 图标

有了这个知识以后。

我们就知道 只需要将项目里面的 带left_xx_.png 的图 复制旋转。并且替换到新的目录 那么就完成了

主要针对左右箭头。。很显眼

import os
from PIL import Image


def main():
    # 遍历所有的目录找出带左右文件的png
    for root, dirs, files in os.walk("/Users/liuan/AndroidStudioProjects/haive/haive-android"):
        for file in files:
            if (file.endswith(".png") and file.__contains__("right")) or (
                    file.__contains__("left") and file.endswith(".png")):
                # # 获取文件名
                name = file.split(".")[0]
                # # 获取文件的路径
                path = os.path.join(root, file)

                # 已有ldrtl 则无需再次进行拼接
                if path.__contains__("ldrtl"):
                    continue
                # 临时文件目录无需进行替换
                if path.__contains__("/build/intermediates"):
                    continue

                parentPath = path.split("/")[-2]

                # drawable-xxhdpi 中间插入 -ldrtl
                # "-"前面插入数据

                newList = []
                index = 0

                for x in parentPath.split("-"):
                    if index == 1:
                        newList.append("ldrtl")
                    newList.append(x)
                    index = index + 1
                # 将数组转换成字符串由"-"连接
                newParentPath = "-".join(newList)
                newFilePath = path.replace(parentPath, newParentPath)

                # 判断newFilePath 父目录是否存在 不存在则创建
                if not os.path.exists(os.path.dirname(newFilePath)):
                    os.makedirs(os.path.dirname(newFilePath))

                if not os.path.exists(newFilePath):
                    # 图片旋转180度并保存为新的路径
                    print("以下文件将会被替换##########################")
                    print(name)
                    print(path)
                    print(newFilePath)
                    im = Image.open(path)
                    im = im.rotate(180)
                    im.save(newFilePath)


if __name__ == '__main__':
    main()

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

安果移不动

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

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

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

打赏作者

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

抵扣说明:

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

余额充值