python自动化生成IOS的图标

所属分类: 脚本专栏 / python 阅读数: 666
收藏 0 赞 0 分享

本文实例为大家分享了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

python进行TCP端口扫描的实现

这篇文章主要介绍了python进行TCP端口扫描的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python简单获取二维数组行列数的方法示例

这篇文章主要介绍了Python简单获取二维数组行列数的方法,结合实例形式分析了Python基于numpy模块的二维数组相关运算技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现的字典排序操作示例【按键名key与键值value排序】

这篇文章主要介绍了Python实现的字典排序操作,结合实例形式分析了Python针对字典分别按照键名key与键值value进行排序的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python类装饰器实现方法详解

这篇文章主要介绍了Python类装饰器实现方法,结合实例形式较为详细的分析了Python类装饰器的相关概念、原理、实现方法与使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用python对文件中的单词进行提取的方法示例

这篇文章主要介绍了使用python对文件中的单词进行提取的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python函数装饰器实现方法详解

这篇文章主要介绍了Python函数装饰器实现方法,结合实例形式较为详细的分析了Python函数装饰器的概念、功能、用法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python 删除字符串中连续多个空格并保留一个的方法

今天小编就为大家分享一篇python 删除字符串中连续多个空格并保留一个的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python 文本单词提取和词频统计的实例

今天小编就为大家分享一篇python 文本单词提取和词频统计的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python装饰器基础概念与用法详解

这篇文章主要介绍了Python装饰器基础概念与用法,结合实例形式详细分析了Python装饰器的概念、功能、用法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

对python 读取线的shp文件实例详解

今天小编就为大家分享一篇对python 读取线的shp文件实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多