Skip to content

Instantly share code, notes, and snippets.

@anthonycastelli
Created February 10, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonycastelli/1e614bf0708821cf5ad0 to your computer and use it in GitHub Desktop.
Save anthonycastelli/1e614bf0708821cf5ad0 to your computer and use it in GitHub Desktop.
iOS 8 Icons
import os
import sys
import numpy
from PIL import Image
if len(sys.argv) < 3:
print('You must specifiy an image and an output folder')
print('i.e. "resizer.py image.png output_folder_name')
sys.exit(1)
# iOS 8 iPhone icon sizes
iphone = {
58: 'icon_iphone_small@2x.png',
87: 'icon_iphone_small@3x.png',
80: 'icon_iphone_spotlight@2x.png',
120: 'icon_iphone_spotlight@3x.png',
120: 'icon_iphone@2x.png',
180: 'icon_iphone@3x.png'
}
# iOS 8 iPad icon sizes
ipad = {
29: 'icon_ipad_small.png',
58: 'icon_ipad_small@2x.png',
40: 'icon_ipad_spotlight.png',
80: 'icon_ipad_spotlight@2x.png',
76: 'icon_ipad.png',
152: 'icon_ipad@2x.png'
}
imageFile = sys.argv[1]
outputFolder = sys.argv[2]
# Define our resizing method
def resizeImage(image, size, output):
img = Image.open(image)
premult = numpy.fromstring(img.tostring(), dtype=numpy.uint8)
alphaLayer = premult[3::4] / 255.0
premult[::4] *= alphaLayer
premult[1::4] *= alphaLayer
premult[2::4] *= alphaLayer
img = Image.fromstring("RGBA", img.size, premult.tostring())
img.resize((size, size), Image.ANTIALIAS).save(output)
# Check and see if the folder already exists
if not os.path.isdir(outputFolder):
os.mkdir(outputFolder)
# Change our current working directory to our output folder
os.chdir(outputFolder)
print('### Converting iPhone Icons ###')
for size in iphone.keys():
print(' Creating %s icon' % iphone[size])
resizeImage(imageFile, size, iphone[size])
print('### Finished Creating iPhone Icons ###')
print('')
print('### Converting iPad Icons ###')
for size in ipad.keys():
print(' Creating %s icon' % ipad[size])
resizeImage(imageFile, size, ipad[size])
print('### Finished Creating iPhone Icons ###')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment