Generate ICONS and screenshots for iOS10 using Python
- 2020-05-12 02:51:41
- OfStack
Introduction to the
After updating Xcode8 in the past two days, I found that the icon requirements of Xcode have changed again. The small app "IconKit" I used before has not caught up with the rhythm and can no longer meet the requirements of Xcode8.
Then I remembered to use Python to make a script to generate ICONS.
In fact, this script was written long ago. Now, in order to adapt to iOS10, it has been modified and improved and put into GitHub.
Check out the renderings:
1.png
Code:
#encoding=utf-8
#by The little lamp that never goes out
#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':
# Android rounded corners
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])# The original size
multiply = int(size.split('@')[1][0:1])# A multiple
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':# To deal with jpg and 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':# The default ios If it's android
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]:# If it's landscape, swap coordinates
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 measured, perhaps is the author too dish, tried various methods to install PIL always wrong, finally still used Pillow, effect 1 kind of.
How to use scripts
At the command line of Windows or the terminal of Mac, enter:
python tool.py [action] [filename] [platform]
action: icon or screenshot
filename: icon file name, screen shot does not need the file name, automatic traversal
platform: ios or android
Here are some examples:
Generate the iOS icon: python tool.py icon icon.jpg ios
Generate android icon: python tool. py icon icon. jpg android
Generate a screenshot of iOS: python tool.py screenshot ios
Generate android screenshots: python tool.py screenshot android
Note:
It takes 1 sheet of PNG to crop the android rounded icon, size 512x512, 70 rounded corner, GitHub has been attached.
When creating screenshots, all JPG and PNG files will be automatically traversed to automatically identify the horizontal and vertical screens
conclusion
If you find it useful, you are welcome to use Star1 in GitHub, as well as to improve it. The code is easy to understand and commented.