Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Last active February 21, 2017 16:23
Show Gist options
  • Save bozdoz/553e9c4de237bb814c549e13f2bad904 to your computer and use it in GitHub Desktop.
Save bozdoz/553e9c4de237bb814c549e13f2bad904 to your computer and use it in GitHub Desktop.
Automate icon/splashscreen creation for PhoneGap
'''
Given an SVG in the same directory ('./res/' by default),
iterates and creates new icons and splashscreens for
the PhoneGap Hello World Template
'''
import sys, os, subprocess
from PIL import Image
def createImages (logo_file_name = 'logo.svg', bg_color = '#ffffff', logo_name = 'project'):
'''
NOTE: phonegap hello world example splashscreens and icons must exist (to use their files for sizes)
* pass an svg filename to use to create jpgs and pngs splashscreens and icons
* pass a bg_color for splashscreens
* pass a logo_name to overwrite 'Default' and 'Drawable' in the default files
'''
realpath = os.path.dirname(os.path.realpath(__file__))
svg_file = os.path.join(realpath, logo_file_name)
print 'SVG file : %s' % svg_file
# ignoring windows and blackberry (add their directories if you care)
icon_android = './icon/android/'
icon_ios = './icon/ios/'
screen_android = './screen/android/'
screen_ios = './screen/ios/'
def replaceIcons (the_dir) :
os.chdir(the_dir)
for filename in os.listdir('.'):
image_file = Image.open(filename)
size = '%dx%d' % image_file.size
filename = filename.replace('drawable', logo_name)
subprocess.call(['convert',
'-density', '300',
'-resize', size,
'-extent', size,
'-gravity', 'center',
'-background', 'none',
svg_file,
filename])
os.chdir(realpath)
def replaceScreen (the_dir) :
os.chdir(the_dir)
for filename in os.listdir('.'):
image_file = Image.open(filename)
width, height = image_file.size
size = '%dx%d' % image_file.size
# needs padding (using 40% for padding)
icon_size = '%dx%d' % (
int(width*0.6),
int(height*0.6),
)
filename = filename.replace('drawable', logo_name)
filename = filename.replace('Default', logo_name)
subprocess.call(['convert',
'-density', '300',
'-resize', icon_size,
'-extent', size,
'-gravity', 'center',
'-background', bg_color,
svg_file,
filename])
os.chdir(realpath)
replaceIcons(icon_android)
replaceIcons(icon_ios)
replaceScreen(screen_android)
replaceScreen(screen_ios)
if __name__ == '__main__':
createImages(*sys.argv[1:])
# example
# createImages abtom.svg #5bc0de abtom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment