利用Python為iOS10產生表徵圖和截屏,pythonios10

來源:互聯網
上載者:User

利用Python為iOS10產生表徵圖和截屏,pythonios10

簡介

這兩天更新完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.comimport osimport sysfrom 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 screenshotif 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一下,也歡迎改進,代碼簡單易懂加了注釋。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.