Skip to content

Instantly share code, notes, and snippets.

@stephenhouser
Forked from paddlefish/resize_icon.py
Last active February 7, 2016 15:48
Show Gist options
  • Save stephenhouser/cdc18be41d2d49ba9292 to your computer and use it in GitHub Desktop.
Save stephenhouser/cdc18be41d2d49ba9292 to your computer and use it in GitHub Desktop.
Resize Vector source image for AppIcon
#!/usr/bin/env python3
# Script to create all required sizes and resolutions of AppIcon files
# from a vector PDF file. Updates the AppIcon.appiconset/Contents.json
#
# Available:
# https://gist.github.com/stephenhouser/cdc18be41d2d49ba9292
#
# Based on: http://blog.paddlefish.net/?p=983
# Updated to include 3x (generic actually) size icon creation.
#
import re
import json
from subprocess import call
from pprint import pprint
# Required File and Directory Locations
source_pdf = 'Assets/AppIcon.pdf'
asset_catalogs = ['Design Show Server/Images.xcassets/AppIcon.appiconset', 'Design Show/Images.xcassets/AppIcon.appiconset']
for iconset in asset_catalogs:
json_data=open(iconset + '/Contents.json')
newdata = []
data = json.load(json_data)
for img in data["images"]:
size = int(re.sub(r'x.+$','', img["size"]))
filename='{idiom:s}_{size:d}_{scale}.png'.format(idiom=img["idiom"],scale=img["scale"],size=size)
filepath='{iconset:s}/{filename:s}'.format(iconset=iconset,filename=filename)
scale_match = re.match(r'(\d+)x', img["scale"])
if scale_match:
size = size * int(scale_match.group(1))
print("scale =", int(scale_match.group(1)))
newdata.append({ "filename" : filename, "size" : img["size"], "idiom" : img["idiom"], "scale" :img["scale"] })
print('{filename:s} size={size:d}'.format(filename=filename,size=size))
call(['sips', '-s', 'format', 'png', '-Z', str(size), '-o', filepath, source_pdf])
json_data.close()
contents_file = open(iconset + '/Contents.json', "w")
contents_file.write( json.dumps({ "images" : newdata, "info" : { "version" : 1, "author" : "andy's python script" }}) )
contents_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment