Skip to content

Instantly share code, notes, and snippets.

@eladc
Created March 28, 2015 20:06
Show Gist options
  • Save eladc/32cbbd79091dfbe34854 to your computer and use it in GitHub Desktop.
Save eladc/32cbbd79091dfbe34854 to your computer and use it in GitHub Desktop.
Convert a png image to different icons for mobile devices
#!/usr/bin/env python
from os import path, makedirs
from glob import glob
from PIL import Image
## list of folders to create
folders = [r'iOS-Icons', r'Android-Icons'] ## list of folders to create
## create folders
for f in folders: ## create folders
if not path.exists(f):
makedirs(f)
## list of iOS icon sizes to convert to
IOSsizes = [[180, 180], [120, 120], [87, 87], [60, 60], [120, 120], [76, 76], [152, 152], [40, 40], [80, 80], [57, 57], [114, 114], [72, 72], [144, 144], [50, 50], [100, 100], [29, 29], [58, 58]]
IOSnames = ['Icon-60@3x', 'Icon-Small-40@3x', 'Icon-Small@3x', 'Icon-60', 'Icon-60@2x', 'Icon-76', 'Icon-76@2x', 'Icon-Small-40', 'Icon-Small-40@2x', 'Icon', 'Icon@2x', 'Icon-72', 'Icon-72@2x', 'Icon-Small-50', 'Icon-Small-50@2x', 'Icon-Small', 'Icon-Small@2x']
## list of Android icon sizes to convert to
AndroidSizes = [[192, 192], [144, 144], [96, 96], [72, 72], [48, 48], [36, 36]]
AndroidNames = ['Icon-xxxhdpi', 'Icon-xxhdpi', 'Icon-xhdpi', 'Icon-hdpi', 'Icon-mdpi', 'Icon-ldpi']
## search for a png file in the root folder
for f in glob('*.png'):
im = Image.open(f)
for s in IOSsizes:
size = s[0], s[1]
sizeText = IOSnames[IOSsizes.index(s)]
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save(r'iOS-Icons\%s.png' % sizeText)
for s in AndroidSizes:
size = s[0], s[1]
sizeText = AndroidNames[AndroidSizes.index(s)]
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save(r'Android-Icons\%s.png' % sizeText)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment