app圖標
先貼腳本地址autoExportAppIcon
1、介紹
作為iOS開發者,或多或少要接觸到app圖標切圖,對于公司沒有美工或者美工對iOS圖標尺寸不了解的iOS開發者來說,每次制作圖標要么自己切圖半天,要么跟美工解釋半天,心也是很累。索性寫了個腳本直接生成圖標,一分鐘換圖標,不要太爽!
2、原理及用法
腳本使用python
寫的,用的庫是有名的PIL
庫。讓美工提供一張不小于1024x1024
的方圖,針對不同尺寸圖標設置resize
圖片,并修改Contents.json
(app圖標信息文件,xcode中需要)。
用法:下載腳本,并在終端下執行python /path/autoExportAppIcon.py /path/image.jpg
終端
icons
在xcode中新建
AppIcon
:
AppIcon
打開
AppIcon
文件夾:
appIcon Folder
Folder
將生成的圖標及
Content.json
文件替換就行了。
3、貼代碼
#!/usr/bin/env python
# coding: utf-8
import sys
import os
try:
from PIL import Image
except:
print ('\033[31m' + '缺少Image模塊,正在安裝Image模塊,請等待...' + '\033[0m')
success = os.system('python -m pip install Image')
if success == 0:
print('\033[7;32m' + 'Image模塊安裝成功.' + '\033[0m')
from PIL import Image
else:
print ('\033[31m' + 'Image安裝失敗,請手動在終端執行:\'python -m pip install Image\'重新安裝.' + '\033[0m')
quit()
outPutPath = os.path.expanduser('~') + '/Desktop/AppIcon/'
if not os.path.exists(outPutPath):
os.mkdir(outPutPath)
if len(sys.argv) <= 1:
print ('\033[31m' + '請輸入圖片路徑,eg: python autoExportAppIcon.py /path/xxx.png' + '\033[0m')
quit()
ImageName = sys.argv[1]
# print('圖片名字為:' + ImageName)
originImg = ''
try:
originImg = Image.open(ImageName)
except:
print ('\033[31m' + '\'' + ImageName + '\'' + ',該文件不是圖片文件,請檢查文件路徑.' + '\033[0m')
quit()
# 20x20
img0 = originImg.resize((20,20), Image.ANTIALIAS)
img1 = originImg.resize((40,40), Image.ANTIALIAS)
img2 = originImg.resize((60,60), Image.ANTIALIAS)
img0.save(outPutPath + 'appIcon20x20.png',"png")
img1.save(outPutPath + 'appIcon20x20@2x.png',"png")
img2.save(outPutPath + 'appIcon20x20@3x.png',"png")
# 29x29
img3 = originImg.resize((29,29), Image.ANTIALIAS)
img4 = originImg.resize((58,58), Image.ANTIALIAS)
img5 = originImg.resize((87,87), Image.ANTIALIAS)
img3.save(outPutPath + 'appIcon29x29.png',"png")
img4.save(outPutPath + 'appIcon29x29@2x.png',"png")
img5.save(outPutPath + 'appIcon29x29@3x.png',"png")
# 40x40
img6 = originImg.resize((40,40), Image.ANTIALIAS)
img7 = originImg.resize((80,80), Image.ANTIALIAS)
img8 = originImg.resize((120,120), Image.ANTIALIAS)
img6.save(outPutPath + 'appIcon40x40.png',"png")
img7.save(outPutPath + 'appIcon40x40@2x.png',"png")
img8.save(outPutPath + 'appIcon40x40@3x.png',"png")
# 60x60
img9 = originImg.resize((120,120), Image.ANTIALIAS)
img10 = originImg.resize((180,180), Image.ANTIALIAS)
img9.save(outPutPath + 'appIcon60x60@2x.png',"png")
img10.save(outPutPath + 'appIcon60x60@3x.png',"png")
# ipad
img11 = originImg.resize((76,76), Image.ANTIALIAS)
img12 = originImg.resize((152,152), Image.ANTIALIAS)
img13 = originImg.resize((167,167), Image.ANTIALIAS)
img11.save(outPutPath + 'appIcon76x76.png',"png")
img12.save(outPutPath + 'appIcon76x76@2x.png',"png")
img13.save(outPutPath + 'appIcon83.5x83.5@2x.png',"png")
# 創建Contents.json文件
content = '''
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"filename" : "appIcon20x20@2x.png",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"filename" : "appIcon20x20@3x.png",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "appIcon29x29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "appIcon29x29@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "appIcon29x29@3x.png",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "appIcon40x40@2x.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "appIcon40x40@3x.png",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "appIcon60x60@2x.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "appIcon60x60@3x.png",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"filename" : "appIcon20x20.png",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"filename" : "appIcon20x20@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "appIcon29x29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "appIcon29x29@2x.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "appIcon40x40.png",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "appIcon40x40@2x.png",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "appIcon76x76.png",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "appIcon76x76@2x.png",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "appIcon83.5x83.5@2x.png",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
'''
f = open(outPutPath + 'Contents.json', 'w')
f.write(content)
print('\033[7;32m' + '文件輸出文件夾:' + outPutPath + '\033[0m')
os.system('open ' + outPutPath)
注:xcode
版本為8.0
如使用有問題,請留言.