Home >Backend Development >Python Tutorial >Python generates icons and screenshots for iOS10

Python generates icons and screenshots for iOS10

高洛峰
高洛峰Original
2017-02-24 15:55:251164browse

Introduction

After updating Xcode8 in the past two days, I found that Xcode’s requirements for icons have changed again, and a small application “IconKit” I used before has not caught up. The rhythm can no longer meet the requirements of Xcode8.

So I thought of using Python to make a script to generate icons.

In fact, this script was written a long time ago. Now in order to adapt to iOS10, it has been modified and improved and put on GitHub.

You can take a look at the renderings:

1.png

Code:

#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" !'

Script environment requirements

Python 2.7

PIL or Pillow

The author personally tested it, maybe I am too good at it, try it I tried various methods to install PIL but always got errors. Finally I used Pillow and the effect was the same.

How to use the script

In the Windows command line or Mac terminal, enter:

python tool.py [action] [ filename] [platform]
action: icon or screenshot
filename: icon file name, no file name is required for screenshots, automatic traversal
platform: ios or android

Give some examples:

Generate iOS icon: python tool.py icon icon.jpg ios

Generate Android icon: python tool.py icon icon.jpg android

Generate iOS screenshots: python tool.py screenshot ios

Generate Android screenshots: python tool.py screenshot android

Note:

To generate an Android rounded corner icon, you need a PNG to be cropped, with a size of 512x512 and a 70mm rounded corner. It is already included in GitHub.

When generating screenshots, it will automatically traverse all JPG and PNG files and automatically identify horizontal and vertical screens

Conclusion

If you find it useful, welcome Star it in GitHub. Improvements are also welcome. The code is simple and easy to understand with comments.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to Python generating icons and screenshots for iOS10, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn