Use Python to generate icons and screenshots for iOS10, and use pythonios10

Source: Internet
Author: User

Use Python to generate icons and screenshots for iOS10, and use pythonios10

Introduction

After updating Xcode8 in the past two days, I found that Xcode's requirements for icons have changed. The previous small application "IconKit" has not kept up with the pace and cannot meet the requirements of Xcode8.

So I thought of using Python to create a script to generate an icon.

In fact, this script has been written very early. Now, in order to adapt to iOS10, it has been modified and released to GitHub.

You can see:

1. png

Code:

# Encoding = UTF-8 # by light # 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 ', '2014 @ 1x'] androidSizes = [167, 144,192,] androidNames = ['ldpi ', 'mdpi ', 'hdpi', 'xhdpi ', 'xxhdpi', 'xxxhdpi '] sizesiOS = [(640,960 ),( 640,113 6), (750,133 4), (1242,220 8), (1536,204 8), (2048,273 2)] foldersiOS = ['iphone4s ', 'iphone5', 'iphone6 ', 'iphone6plus ', 'ipad', 'ipadlarge '] sizesAndroid = [(480,800), ()] 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 corner mask = Image.open('mask.png ') r, g, B, a = mask. split () icon. putalpha (a) if not OS. path. isdir ('androdicon '): OS. mkdir ('androdicon ') index = 0 for size in androidSizes: im = icon. resize (size, size), Image. BILINEAR) im. save ('androidcon/icon-'+ androidnames?index='.png') index = index + 1 else: if not OS. path. isdir ('iosicon '): OS. mkdir ('iosicon ') for size in io SSizes: originalSize = int (size. split ('@') [0]) # original size multiply = int (size. split ('@') [1] []) # multiple im = icon. resize (originalSize * multiply, originalSize * multiply), Image. BILINEAR) im. save ('iosicon/icon'{size}'.png ') print 'Congratulations! It \'s all done! 'Def cmd_dir (dir, platform): files = OS. listdir (dir) for name in files: if name. split ('. ') [-1] = 'jpg' or name. split ('. ') [-1] = 'png': # process 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': # default ios, if it is 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 is a horizontal screen, 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 screenshotif action = 'screenshot ': platform = sys. argv [2] # platform if platform = 'ios ': pai_dir ('. /', 'ios') elif platform = 'android': pai_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 1, 2.7

PIL or Pillow

I personally tested it. Maybe it was my advice. I tried various methods to install PIL, and I always encountered an error. Finally, I used Pillow, with the same effect.

How to use scripts

On a Windows command line or Mac terminal, enter:

Python tool. py [action] [filename] [platform]
Action: icon or screenshot
Filename: indicates the icon file name. The screenshot does not need a file name and is automatically traversed.
Platform: ios or android

For example:

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

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

Generate a screenshot for iOS: python tool. py screenshot ios

Generate a screenshot of android: python tool. py screenshot android

Note:

To generate an android rounded corner icon, you need a PNG Image to crop the image. The image size is 512x512, and the image size is 70. It is included in GitHub.

When a screenshot is generated, all JPG and PNG files are automatically traversed and the screen is automatically recognized.

Conclusion

If you find it useful, welcome to Star in GitHub, and welcome improvements. The code is easy to understand and annotated.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.