简介
这两天更新完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 = [‘480×800′,’720×1280′,’1080×1920’]
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 或者 screenshotfilename:图标文件名,截屏不需要文件名,自动遍历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来裁剪,尺寸512×512,70圆角,github中已经附带。
生成截屏时会自动遍历所有jpg和png文件,自动识别横竖屏
结语
如果你觉得有用,欢迎在github中star一下,也欢迎改进,代码简单易懂加了注释。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持php中文网。
更多python为ios10生成图标和截屏相关文章请关注php中文网!