Skip to content

Instantly share code, notes, and snippets.

@pwc3
Created March 27, 2020 17:42
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 pwc3/7806daf83092790b71fb3a2f2bde4d16 to your computer and use it in GitHub Desktop.
Save pwc3/7806daf83092790b71fb3a2f2bde4d16 to your computer and use it in GitHub Desktop.
Converts a PDF to an iOS app icon
#!/usr/bin/env python
import argparse
import codecs
import json
import subprocess
import sys
def parse_args(argv):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description="")
parser.add_argument("-d", "--dry-run",
default=False,
action="store_true",
help="Dry run only")
parser.add_argument("pdf_file",
metavar="FILE",
help="input filename")
return parser.parse_args(argv)
class Icon:
def __init__(self, name, idiom, point_size, scale):
self.name = name.lower().replace(" ", "_")
self.idiom = idiom
self.point_size = point_size
self.scale = scale
def pixel_size(self):
return self.point_size * self.scale
def formatted_point_size(self):
if isinstance(self.point_size, float):
return "%.1f" % self.point_size
else:
return "%d" % self.point_size
def formatted_pixel_size(self):
pixel_size = self.pixel_size()
return "%dx%d" % (pixel_size, pixel_size)
def formatted_scale(self):
return "%dx" % self.scale
def point_dimensions(self):
s = self.formatted_point_size()
return "%sx%s" % (s, s)
def pixel_dimensions(self):
s = self.formatted_pixel_size()
return "%sx%x" % (s, s)
def filename(self):
return "icon_%s_%s@%s.png" % (self.name, self.formatted_point_size(), self.formatted_scale())
def generate(self, pdf, is_dry_run):
print "Creating %s" % self.filename()
cmd = ["convert", "-density", "400", pdf, "-scale", self.formatted_pixel_size(), self.filename()]
if is_dry_run:
print cmd
else:
subprocess.check_call(cmd)
def json(self):
return {
"filename": self.filename(),
"idiom": self.idiom,
"scale": self.formatted_scale(),
"size": self.point_dimensions()
}
def icons(name, idiom, point_size, scales):
return map(lambda x: Icon(name, idiom, point_size, x), scales)
def main(argv=None):
options = parse_args(argv)
images = []
images.extend(icons("iPhone Notification", "iphone", 20, [2, 3]))
images.extend(icons("iPhone Settings", "iphone", 29, [2, 3]))
images.extend(icons("iPhone Spotlight", "iphone", 40, [2, 3]))
images.extend(icons("iPhone App", "iphone", 60, [2, 3]))
images.extend(icons("iPad Notification", "ipad", 20, [1, 2]))
images.extend(icons("iPad Settings", "ipad", 29, [1, 2]))
images.extend(icons("iPad Spotlight", "ipad", 40, [1, 2]))
images.extend(icons("iPad App", "ipad", 76, [1, 2]))
images.extend(icons("iPad Pro App", "ipad", 83.5, [2]))
images.extend(icons("App Store", "ios-marketing", 1024, [1]))
for icon in images:
icon.generate(options.pdf_file, options.dry_run)
json_obj = {
"images": map(lambda x: x.json(), images),
"info": {
"author": "xcode",
"version": 1
}
}
json_str = json.dumps(json_obj, indent=True, sort_keys=True)
if options.dry_run:
print json_str
else:
with codecs.open("Contents.json", "w", "utf-8") as fh:
print >>fh, json_str
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment