python 自动化生成IOS的图标

每次上架之前都要生成十几个图片感觉无聊麻烦,考虑使用脚本处理
脚本使用python 和一部分shell 处理的,python部分主要是使用PIL库处理图片,和调用shell脚本,shell 主要是操作文件

#coding=utf-8
import os ,threading
from PIL import Image
import subprocess
import json
class ImgManager(object):
    thread_lock = threading.Lock()
    @classmethod
    def sharedinstance(cls):
        with ImgManager.thread_lock:
            if not hasattr(ImgManager,"instance"):
                ImgManager.instance = ImgManager()
        return ImgManager.instance

    # 运行shell命令
    def runshellCMD(self,cmd,dsr):
        progress = subprocess.Popen(cmd,shell=True)
        progress.wait()
        result = progress.returncode
        if result !=0:
            print("%s失败"%(dsr))
        else:
            print("%s成功"%(dsr))

	#创建图片
    def createImg(self,model):
        path = '%s/AppStore.png'%(os.getcwd())
        currentPath = "%s/Images/%s"%(os.getcwd(),model.filename)
        print(currentPath)
        im = Image.open(path,'r')
        # w,h=im.size
        # print("%s,%s"%(str(w),str(h)))
        #
        im.thumbnail((float(model.get_wh()),float(model.get_wh())))
        if model.filename.endswith('.png'):
            im.save("%s" % (currentPath),"png")
        else:
            # self.runshellCMD("sudo cp %s %s" % (path, currentPath), "拷贝")
            self.addTransparency(im)
            im.save("%s" % (currentPath), "jpeg")
            # r, g, b, alpha = im.split()
            # print("%s"%(str(im.split()[0])))

#修改透明度
    def addTransparency(img, factor=0.0):
        img = img.convert('RGBA')
        img_blender = Image.new('RGBA', img.size, (0, 0, 256, 256))
        img = Image.blend(img_blender, img, factor)
        return img


#解析Contents.json,这个文件每一个Images.xcassets 的AppIcon文件夹都有,直接复用就可以了
    def handle_icon_images(self):

        jsonpath = os.getcwd() +"/Contents.json"
        if not os.path.exists(jsonpath):
            print("Contents.json path not exite")
            return
        with open(jsonpath,'r') as f:
            jsonstr = f.read()
        modle = json.loads(jsonstr)
        arrs =  modle['images']
        # print(arrs)
        icon_models=[]
        for obj in arrs:
            size=obj["size"]
            idiom=obj["idiom"]
            filename=obj["filename"]
            scale=obj["scale"]
            icom =iconImg(size=size,idiom=idiom,filename=filename,scale=scale)
            # icon_models.append(icom)
            self.createImg(icom)


    """

    "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "Icon-Small@3x.png",
      "scale" : "3x"
    """
    #json 数据里面有效数据的类
class iconImg(object):
    def __init__(self,size,idiom,filename,scale):
        self.size = size
        self.idiom = idiom
        self.filename = filename
        self.scale = scale

    def show(self):
        print("%s,%s,%s,%s"%(self.size,self.idiom,self.filename,self.scale))


    def get_wh(self):
        return (float(self.size.split('x')[0]))*(float(self.scale.split('x')[0]))



if __name__ == '__main__':
    ImgManager.sharedinstance().handle_icon_images()




新版的Content.json 后面却少了一部分 filename

{
  "images" : [
    {
      "size" : "20x20",
      "idiom" : "iphone",
      "filename" : "Icon-Notification@2x-1.png",
      "scale" : "2x"
    },
    {
      "size" : "20x20",
      "idiom" : "iphone",
      "filename" : "Icon-Notification@3x.png",
      "scale" : "3x"
    },
    {
      "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "Icon-Small-1.png",
      "scale" : "1x"
    },
    {
      "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "Icon-Small@2x-1.png",
      "scale" : "2x"
    },
    {
      "size" : "29x29",
      "idiom" : "iphone",
      "filename" : "Icon-Small@3x.png",
      "scale" : "3x"
    },
    {
      "size" : "40x40",
      "idiom" : "iphone",
      "filename" : "Icon-Small-40@2x-1.png",
      "scale" : "2x"
    },
    {
      "size" : "40x40",
      "idiom" : "iphone",
      "filename" : "Icon-Small-40@3x.png",
      "scale" : "3x"
    },
    {
      "size" : "57x57",
      "idiom" : "iphone",
      "filename" : "Icon.png",
      "scale" : "1x"
    },
    {
      "size" : "57x57",
      "idiom" : "iphone",
      "filename" : "Icon@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "60x60",
      "idiom" : "iphone",
      "filename" : "Icon-60@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "60x60",
      "idiom" : "iphone",
      "filename" : "Icon-60@3x.png",
      "scale" : "3x"
    },
    {
      "size" : "20x20",
      "idiom" : "ipad",
      "filename" : "Icon-Notification.png",
      "scale" : "1x"
    },
    {
      "size" : "20x20",
      "idiom" : "ipad",
      "filename" : "Icon-Notification@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "29x29",
      "idiom" : "ipad",
      "filename" : "Icon-Small.png",
      "scale" : "1x"
    },
    {
      "size" : "29x29",
      "idiom" : "ipad",
      "filename" : "Icon-Small@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "40x40",
      "idiom" : "ipad",
      "filename" : "Icon-Small-40.png",
      "scale" : "1x"
    },
    {
      "size" : "40x40",
      "idiom" : "ipad",
      "filename" : "Icon-Small-40@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "50x50",
      "idiom" : "ipad",
      "filename" : "Icon-Small-50.png",
      "scale" : "1x"
    },
    {
      "size" : "50x50",
      "idiom" : "ipad",
      "filename" : "Icon-Small-50@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "72x72",
      "idiom" : "ipad",
      "filename" : "Icon-72.png",
      "scale" : "1x"
    },
    {
      "size" : "72x72",
      "idiom" : "ipad",
      "filename" : "Icon-72@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "76x76",
      "idiom" : "ipad",
      "filename" : "Icon-76.png",
      "scale" : "1x"
    },
    {
      "size" : "76x76",
      "idiom" : "ipad",
      "filename" : "Icon-76@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "83.5x83.5",
      "idiom" : "ipad",
      "filename" : "Icon-83.5@2x.png",
      "scale" : "2x"
    },
    {
      "size" : "1024x1024",
      "idiom" : "ios-marketing",
      "filename" : "icon.jpg",
      "scale" : "1x"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值