Last Updated: September 09, 2019
·
1.583K
· patrickocoffeyo

Use Python to Create Apple Touch Icons

When building a web application, it's important to add apple-touch-icons to the project so that it can be bookmarked on the home screen of your users' devices.

I got tired of having create a touch icon for each size in photoshop, so I wrote a quick little python script to do it for me:

#!/usr/bin/python

import sys
import optparse
import os
import Image

def main():
  p = optparse.OptionParser()
  p.add_option('--source', '-s', default='')
  p.add_option('--output', '-o', default='touch-icons/')
  options, arguments = p.parse_args()

  if not os.path.exists(options.output):
    os.system('mkdir '+options.output)

  image = Image.open(options.source)
  ext = 'png'

  #apple-touch-icons
  sizes = [57,72,57,114,144]
  for size in sizes:
    sizeStr = str(size)
    newImage = image.resize((size, size), Image.ANTIALIAS)
    newImage.save(options.output+'apple-touch-icon'+sizeStr+'x'+sizeStr+'-precomposed.png')


if __name__ == '__main__':
  main()

If you stick this in a shell file, you should be able to run the script, pass it a path to a source file (square and larger then 144px x 144px) and it will spit out all the sizes you need into an output folder.

Seems like a lot of code to save a little time, but I like writing scripts to do random annoying tasks for me :)

1 Response
Add your response

Note that you will need to install Pillow in order to use this script :) (the import Image part that is).

If you already have PIL (the python image library) installed, you are also good to go. Pillow is just a friendly (modern) fork with some bug fixes and easier installation :)

over 1 year ago ·