黑松山资源网 Design By www.paidiu.com
简介
这两天更新完Xcode8之后发现Xcode对图标的要求又有了变化,之前用的一个小应用“IconKit”还没赶上节奏,已经不能满足Xcode8的要求了。
于是就想起来用Python自己做个脚本来生成图标。
其实这个脚本很早就写了,现在为了适应iOS10,就修改完善下,并且放到了GitHub。
可以看看效果图:
1.png
代码:
#encoding=utf-8 #by 不灭的小灯灯 #create date 2016/5/22 #update 2016/9/21 #support iOS 10 #site www.winterfeel.com import os import sys from PIL import Image iosSizes = ['20@1x','20@2x','20@3x','29@1x','29@2x','29@3x','40@1x','40@2x','40@3x','60@2x','60@3x','60@3x','76@1x','76@2x','167@1x'] androidSizes = [32,48,72,96,144,192] androidNames = ['ldpi','mdpi','hdpi','xhdpi','xxhdpi','xxxhdpi'] sizesiOS = [(640,960),(640, 1136),(750, 1334),(1242, 2208),(1536, 2048),(2048, 2732)] foldersiOS = ['iPhone4s','iPhone5','iPhone6','iPhone6plus','iPad','iPadLarge'] sizesAndroid = [(480,800),(720,1280),(1080,1920)] foldersAndroid = ['480x800','720x1280','1080x1920'] def processIcon(filename,platform): icon = Image.open(filename).convert("RGBA") if icon.size[0] != icon.size[1]: print 'Icon file must be a rectangle!' return if platform == 'android': #安卓圆角 mask = Image.open('mask.png') r,g,b,a = mask.split() icon.putalpha(a) if not os.path.isdir('androidIcon'): os.mkdir('androidIcon') index = 0 for size in androidSizes: im = icon.resize((size,size),Image.BILINEAR) im.save('androidIcon/icon-'+ androidNames[index]+'.png') index = index + 1 else: if not os.path.isdir('iosIcon'): os.mkdir('iosIcon') for size in iosSizes: originalSize = int(size.split('@')[0])#原始尺寸 multiply = int(size.split('@')[1][0:1])#倍数 im = icon.resize((originalSize*multiply,originalSize*multiply),Image.BILINEAR) im.save('iosIcon/icon'+size+'.png') print 'Congratulations!It\'s all done!' def walk_dir(dir,platform): files = os.listdir(dir) for name in files: if name.split('.')[-1] == 'jpg' or name.split('.')[-1] == 'png':#处理jpg和png produceImage(name,platform) print 'Congratulations!It\'s all done!' def produceImage(filename,platform): print 'Processing:' + filename img = Image.open(filename) index = 0 sizes = sizesiOS folders = foldersiOS if platform == 'android':#默认ios,如果是安卓 sizes = sizesAndroid folders = foldersAndroid for size in sizes: if not os.path.isdir(folders[index]): os.mkdir(folders[index]) if img.size[0] > img.size[1]:#如果是横屏,交换坐标 im = img.resize((size[1],size[0]),Image.BILINEAR) im.save(folders[index]+'/'+filename) else: im = img.resize(size,Image.BILINEAR) im.save(folders[index]+'/'+filename) index = index + 1 action = sys.argv[1]#action:icon or screenshot if action == 'screenshot': platform = sys.argv[2]#platform if platform == 'ios': walk_dir('./','ios') elif platform == 'android': walk_dir('./','android') else: print 'Hey,Platform can only be "ios" or "android" !' elif action == 'icon': filename = sys.argv[2]#image filename platform = sys.argv[3]#platform if not os.path.exists(filename): print 'Hey,File Not Found!' else: if platform == 'ios': processIcon(filename,'ios') elif platform == 'android': processIcon(filename,'android') else: print 'Hey,Platform can only be "ios" or "android" !' else: print 'Hey,action can only be "icon" or "screenshot" !'
脚本环境要求
Python 2.7
PIL or Pillow
笔者亲测,也许是笔者太菜,试了各种方法安装PIL总是出错,最后还是用了Pillow,效果一样的。
怎么使用脚本
在Windows的命令行或者Mac的终端,输入:
python tool.py [action] [filename] [platform]
action:icon 或者 screenshot
filename:图标文件名,截屏不需要文件名,自动遍历
platform:ios 或者 android
举一些例子:
生成iOS的图标:python tool.py icon icon.jpg ios
生成安卓的图标:python tool.py icon icon.jpg android
生成iOS的截屏:python tool.py screenshot ios
生成安卓的截屏:python tool.py screenshot android
注意:
生成安卓圆角图标需要一张PNG来裁剪,尺寸512x512,70圆角,GitHub中已经附带。
生成截屏时会自动遍历所有JPG和PNG文件,自动识别横竖屏
结语
如果你觉得有用,欢迎在GitHub中Star一下,也欢迎改进,代码简单易懂加了注释。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- s14全球总决赛瑞士轮赛制是什么 全球总决赛瑞士轮赛制详情
- 《COD21》全新剧情宣传片:10月25日XGP见!
- 美国拳王家门口身中7枪遇害 年仅31岁凶手仍未被逮捕
- 《寂静岭2》重制版光追对比:氛围更加恐怖
- 林慧萍.1982-往昔(飞跃复刻版)【歌林】【WAV+CUE】
- 陈楚生.2007-原来我一直都不孤单(EP)【天娱传媒】【WAV+CUE】
- 罗嘉良.1986-秦始皇(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 2024周年庆赠送物品介绍,周年庆送什么
- 【原神】V5.0攻略 |「明黄曜石断片」全收集
- 【原神】V5.0攻略 | 隐藏成就「烦请高抬贵脚」达成
- 证声音乐图书馆《蔚蓝海岸·爵士度假日》[FLAC/分轨][155.47MB]
- 腾讯音乐人《浪漫火焰(新势力计划合辑 Vol.11)》[320K/MP3][56.91MB]
- 腾讯音乐人《浪漫火焰(新势力计划合辑 Vol.11)[FLAC/分轨][147.45MB]
- 2024年,哪里还可以玩有趣的魔兽RPG?
- 类银河恶魔城动作游戏《灵魂熔解》已正式登陆Steam