Use Python to generate icons and screenshots for iOS10 _python

Source: Internet
Author: User
Tags mkdir

Brief introduction

The two days after the update Xcode8 found that Xcode to the icon requirements have changed, used before a small application of "Iconkit" has not caught up with the rhythm, can not meet the Xcode8 requirements.

So I thought of using Python to make my own script to generate an icon.

In fact, this script is written very early, now in order to adapt to iOS10, on the revised and perfected, and put to the GitHub.

You can look at the effect chart:

1.png

Code:

#encoding =utf-8 #by Lights #create date 2016/5/22 #update 2016/9/21 #support iOS #site www.winterfeel.com import os im Port 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.op
    En (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.putalph
    A (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 (' IO Sicon '): Os.mkdir (' Iosicon ') for the size in iossizes:originalsize = Int (size.split (' @ ') [0]) #原始尺寸 Multi ply = 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 = s
Izesios folders = Foldersios if platform = = ' Android ': #默认ios, if it is an 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]: #如果是横屏, exchange coordinates im = Img.resize ((size[1],size[0)), Image.bi LINEAR) Im.save (folders[index]+ '/' +filename) Else:im = Img.resize (size,image.bilinear) im.save (fold   
  ers[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 ': WA  Lk_dir ('./', ' Android ') Else:print ' hey,platform can only ' 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 (Filena Me, ' Android ')
    Else:print ' Hey,platform can only be "ios" or "Android"! ' Else:print ' hey,action can is ' icon ' or ' SC
 Reenshot "!"

Scripting environment Requirements

Python 2.7

PiL or Pillow

The author of the test, perhaps the author too, tried a variety of methods to install PIL is always wrong, and finally used the pillow, the effect of the same.

How to use a script

At the command line of Windows or at the end of the Mac, enter:

Python tool.py [action] [filename] [platform]
Action:icon or screenshot.
FileName: Icon filename, screenshot does not require file name, automatic traversal
Platform:ios or Android

Give some examples:

Create iOS icon: Python tool.py icon icon.jpg iOS

Build android Icon: Python tool.py icon icon.jpg android

To generate a screenshot of iOS: Python tool.py screenshot ios

Build Android Screenshots: Python tool.py screenshot android

Attention:

The creation of an Android fillet icon requires a PNG to be cropped, the dimensions 512x512,70 rounded, and the GitHub is already attached.

All JPG and PNG files are automatically traversed when the screenshot is generated, and the screen is automatically identified.

Conclusion

If you find it useful, welcome to star in GitHub, also welcome the improvement, the code is easy to understand and add notes.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.