注明:該文章很大程度上參考下面的“參考鏈接”:
參考鏈接
源碼鏈接(必須給原作者一個(gè)贊)
環(huán)境的安裝注意;
python 中的PIL的配置;
若是上面執(zhí)行有誤,可以查看下面的參考鏈接
參考鏈接
Note:按照這個(gè)格式:
python tool.py [action] [filename] [platform]
action:icon 或者 screenshot
filename:圖標(biāo)文件名,截屏不需要文件名,自動(dòng)遍歷
platform:ios 或者 android
eg:
生成iOS的圖標(biāo):python tool.py icon mask.png ios
生成安卓的圖標(biāo):python tool.py icon mask.png android
生成iOS的截屏:python tool.py screenshot ios
生成安卓的截屏:python tool.py screenshot android
生成安卓圓角圖標(biāo)需要一張PNG來(lái)裁剪,尺寸512x512,70圓角
生成截屏?xí)r會(huì)自動(dòng)遍歷所有JPG和PNG文件,自動(dòng)識(shí)別橫豎屏
可以通過(guò)上面的鏈接:
該文章,很大程度上參考上面的參考連接的;
覺(jué)得不錯(cuò),給個(gè)喜歡!!!
提示:
我這里的平臺(tái)是在mac上進(jìn)行的,若是在其他的平臺(tái)應(yīng)該差別不大;
可以安裝相應(yīng)的python,有2、3 版本,
python3下載連鏈接,若是mac直接下載pkg安裝文件就好
下面是對(duì)應(yīng)的源碼tool.py
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])#倍數(shù)
im = icon.resize((originalSizemultiply,originalSizemultiply),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':#默認(rèn)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]:#如果是橫屏,交換坐標(biāo)
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" !'